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
9483e549c3
commit
00d6839d17
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:913662e78e4e0a2e83e289d74c42e197b1d5af6c2f589057de821734b0141c07"
|
||||
"signature": "sha256:7eaf1c17c09445d2807a0560a73437a138201e67de0b44e98b6c853eff059061"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -43,7 +43,8 @@
|
|||
"- [Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity](#python_small_int)\n",
|
||||
"- [Shallow vs. deep copies if list contains other structures and objects](#shallow_vs_deep)\n",
|
||||
"- [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)"
|
||||
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)\n",
|
||||
"- [Be aware of the consuming generator](#consuming_generator)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -466,6 +467,74 @@
|
|||
],
|
||||
"prompt_number": 1
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<br>\n",
|
||||
"<br>\n",
|
||||
"<a name='consuming_generators'></a>\n",
|
||||
"\n",
|
||||
"## Be aware of the consuming generator\n",
|
||||
"\n",
|
||||
"Be aware using `in` checks with generators, since they won't evaluate from the beginning once a position is \"consumed\"."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"gen = (i for i in range(5))\n",
|
||||
"print('2 in gen,', 2 in gen)\n",
|
||||
"print('3 in gen,', 3 in gen)\n",
|
||||
"print('1 in gen,', 1 in gen) "
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"2 in gen, True\n",
|
||||
"3 in gen, True\n",
|
||||
"1 in gen, False\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 9
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**We can circumvent this problem by using a simple list, though:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"l = [i for i in range(5)]\n",
|
||||
"print('2 in l,', 2 in l)\n",
|
||||
"print('3 in l,', 3 in l)\n",
|
||||
"print('1 in l,', 1 in l) "
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"2 in l, True\n",
|
||||
"3 in l, True\n",
|
||||
"1 in l, True\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 10
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:913662e78e4e0a2e83e289d74c42e197b1d5af6c2f589057de821734b0141c07"
|
||||
"signature": "sha256:7eaf1c17c09445d2807a0560a73437a138201e67de0b44e98b6c853eff059061"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -43,7 +43,8 @@
|
|||
"- [Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity](#python_small_int)\n",
|
||||
"- [Shallow vs. deep copies if list contains other structures and objects](#shallow_vs_deep)\n",
|
||||
"- [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)"
|
||||
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)\n",
|
||||
"- [Be aware of the consuming generator](#consuming_generator)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -466,6 +467,74 @@
|
|||
],
|
||||
"prompt_number": 1
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<br>\n",
|
||||
"<br>\n",
|
||||
"<a name='consuming_generators'></a>\n",
|
||||
"\n",
|
||||
"## Be aware of the consuming generator\n",
|
||||
"\n",
|
||||
"Be aware using `in` checks with generators, since they won't evaluate from the beginning once a position is \"consumed\"."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"gen = (i for i in range(5))\n",
|
||||
"print('2 in gen,', 2 in gen)\n",
|
||||
"print('3 in gen,', 3 in gen)\n",
|
||||
"print('1 in gen,', 1 in gen) "
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"2 in gen, True\n",
|
||||
"3 in gen, True\n",
|
||||
"1 in gen, False\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 9
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**We can circumvent this problem by using a simple list, though:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"l = [i for i in range(5)]\n",
|
||||
"print('2 in l,', 2 in l)\n",
|
||||
"print('3 in l,', 3 in l)\n",
|
||||
"print('1 in l,', 1 in l) "
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stdout",
|
||||
"text": [
|
||||
"2 in l, True\n",
|
||||
"3 in l, True\n",
|
||||
"1 in l, True\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 10
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
|
|
Loading…
Reference in New Issue
Block a user