mirror of
https://github.com/rasbt/python_reference.git
synced 2024-11-27 22:11:13 +00:00
mutable objects as default arguments
This commit is contained in:
parent
e361912537
commit
38092ac583
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:6f669267e7ec7d07f6bd0ac72d891bdb4881c22c9396efcc19ca5bff94903cf9"
|
||||
"signature": "sha256:faa74a34746bf250ef2d72e308074083ee5e60789203d70f630f8c67a709e6fe"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -45,7 +45,8 @@
|
|||
"- [Picking True values from and and or expressions](#false_true_expressions)\n",
|
||||
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)\n",
|
||||
"- [Be aware of the consuming generator](#consuming_generator)\n",
|
||||
"- [`bool` is a subclass of `int`](#bool_int)"
|
||||
"- [`bool` is a subclass of `int`](#bool_int)\n",
|
||||
"- [About lambda and closures-in-a-loop pitfall](#lambda_closure)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -583,6 +584,76 @@
|
|||
],
|
||||
"prompt_number": 16
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<br>\n",
|
||||
"<br>\n",
|
||||
"<a name='lambda_closure'></a>\n",
|
||||
"\n",
|
||||
"## About lambda and closures-in-a-loop pitfall\n",
|
||||
"\n",
|
||||
"The following example illustrates how the (last) `lambda` is being reused:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"my_list = [lambda: i for i in range(5)]\n",
|
||||
"for l in my_list:\n",
|
||||
" print(l())"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"4\n",
|
||||
"4\n",
|
||||
"4\n",
|
||||
"4\n",
|
||||
"4\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 24
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Here, a generator can save you some pain:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"my_gen = (lambda: n for n in range(5))\n",
|
||||
"for l in my_gen:\n",
|
||||
" print(l())"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"0\n",
|
||||
"1\n",
|
||||
"2\n",
|
||||
"3\n",
|
||||
"4\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 25
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:6f669267e7ec7d07f6bd0ac72d891bdb4881c22c9396efcc19ca5bff94903cf9"
|
||||
"signature": "sha256:faa74a34746bf250ef2d72e308074083ee5e60789203d70f630f8c67a709e6fe"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -45,7 +45,8 @@
|
|||
"- [Picking True values from and and or expressions](#false_true_expressions)\n",
|
||||
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)\n",
|
||||
"- [Be aware of the consuming generator](#consuming_generator)\n",
|
||||
"- [`bool` is a subclass of `int`](#bool_int)"
|
||||
"- [`bool` is a subclass of `int`](#bool_int)\n",
|
||||
"- [About lambda and closures-in-a-loop pitfall](#lambda_closure)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -583,6 +584,76 @@
|
|||
],
|
||||
"prompt_number": 16
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<br>\n",
|
||||
"<br>\n",
|
||||
"<a name='lambda_closure'></a>\n",
|
||||
"\n",
|
||||
"## About lambda and closures-in-a-loop pitfall\n",
|
||||
"\n",
|
||||
"The following example illustrates how the (last) `lambda` is being reused:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"my_list = [lambda: i for i in range(5)]\n",
|
||||
"for l in my_list:\n",
|
||||
" print(l())"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"4\n",
|
||||
"4\n",
|
||||
"4\n",
|
||||
"4\n",
|
||||
"4\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 24
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Here, a generator can save you some pain:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"my_gen = (lambda: n for n in range(5))\n",
|
||||
"for l in my_gen:\n",
|
||||
" print(l())"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"0\n",
|
||||
"1\n",
|
||||
"2\n",
|
||||
"3\n",
|
||||
"4\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 25
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
|
|
Loading…
Reference in New Issue
Block a user