Skip to content

Commit 00d6839

Browse files
committed
mutable objects as default arguments
1 parent 9483e54 commit 00d6839

2 files changed

Lines changed: 142 additions & 4 deletions

File tree

.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:913662e78e4e0a2e83e289d74c42e197b1d5af6c2f589057de821734b0141c07"
4+
"signature": "sha256:7eaf1c17c09445d2807a0560a73437a138201e67de0b44e98b6c853eff059061"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -43,7 +43,8 @@
4343
"- [Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity](#python_small_int)\n",
4444
"- [Shallow vs. deep copies if list contains other structures and objects](#shallow_vs_deep)\n",
4545
"- [Picking True values from and and or expressions](#false_true_expressions)\n",
46-
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)"
46+
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)\n",
47+
"- [Be aware of the consuming generator](#consuming_generator)"
4748
]
4849
},
4950
{
@@ -466,6 +467,74 @@
466467
],
467468
"prompt_number": 1
468469
},
470+
{
471+
"cell_type": "markdown",
472+
"metadata": {},
473+
"source": [
474+
"<br>\n",
475+
"<br>\n",
476+
"<a name='consuming_generators'></a>\n",
477+
"\n",
478+
"## Be aware of the consuming generator\n",
479+
"\n",
480+
"Be aware using `in` checks with generators, since they won't evaluate from the beginning once a position is \"consumed\"."
481+
]
482+
},
483+
{
484+
"cell_type": "code",
485+
"collapsed": false,
486+
"input": [
487+
"gen = (i for i in range(5))\n",
488+
"print('2 in gen,', 2 in gen)\n",
489+
"print('3 in gen,', 3 in gen)\n",
490+
"print('1 in gen,', 1 in gen) "
491+
],
492+
"language": "python",
493+
"metadata": {},
494+
"outputs": [
495+
{
496+
"output_type": "stream",
497+
"stream": "stdout",
498+
"text": [
499+
"2 in gen, True\n",
500+
"3 in gen, True\n",
501+
"1 in gen, False\n"
502+
]
503+
}
504+
],
505+
"prompt_number": 9
506+
},
507+
{
508+
"cell_type": "markdown",
509+
"metadata": {},
510+
"source": [
511+
"**We can circumvent this problem by using a simple list, though:**"
512+
]
513+
},
514+
{
515+
"cell_type": "code",
516+
"collapsed": false,
517+
"input": [
518+
"l = [i for i in range(5)]\n",
519+
"print('2 in l,', 2 in l)\n",
520+
"print('3 in l,', 3 in l)\n",
521+
"print('1 in l,', 1 in l) "
522+
],
523+
"language": "python",
524+
"metadata": {},
525+
"outputs": [
526+
{
527+
"output_type": "stream",
528+
"stream": "stdout",
529+
"text": [
530+
"2 in l, True\n",
531+
"3 in l, True\n",
532+
"1 in l, True\n"
533+
]
534+
}
535+
],
536+
"prompt_number": 10
537+
},
469538
{
470539
"cell_type": "code",
471540
"collapsed": false,

not_so_obvious_python_stuff.ipynb

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:913662e78e4e0a2e83e289d74c42e197b1d5af6c2f589057de821734b0141c07"
4+
"signature": "sha256:7eaf1c17c09445d2807a0560a73437a138201e67de0b44e98b6c853eff059061"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -43,7 +43,8 @@
4343
"- [Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity](#python_small_int)\n",
4444
"- [Shallow vs. deep copies if list contains other structures and objects](#shallow_vs_deep)\n",
4545
"- [Picking True values from and and or expressions](#false_true_expressions)\n",
46-
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)"
46+
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)\n",
47+
"- [Be aware of the consuming generator](#consuming_generator)"
4748
]
4849
},
4950
{
@@ -466,6 +467,74 @@
466467
],
467468
"prompt_number": 1
468469
},
470+
{
471+
"cell_type": "markdown",
472+
"metadata": {},
473+
"source": [
474+
"<br>\n",
475+
"<br>\n",
476+
"<a name='consuming_generators'></a>\n",
477+
"\n",
478+
"## Be aware of the consuming generator\n",
479+
"\n",
480+
"Be aware using `in` checks with generators, since they won't evaluate from the beginning once a position is \"consumed\"."
481+
]
482+
},
483+
{
484+
"cell_type": "code",
485+
"collapsed": false,
486+
"input": [
487+
"gen = (i for i in range(5))\n",
488+
"print('2 in gen,', 2 in gen)\n",
489+
"print('3 in gen,', 3 in gen)\n",
490+
"print('1 in gen,', 1 in gen) "
491+
],
492+
"language": "python",
493+
"metadata": {},
494+
"outputs": [
495+
{
496+
"output_type": "stream",
497+
"stream": "stdout",
498+
"text": [
499+
"2 in gen, True\n",
500+
"3 in gen, True\n",
501+
"1 in gen, False\n"
502+
]
503+
}
504+
],
505+
"prompt_number": 9
506+
},
507+
{
508+
"cell_type": "markdown",
509+
"metadata": {},
510+
"source": [
511+
"**We can circumvent this problem by using a simple list, though:**"
512+
]
513+
},
514+
{
515+
"cell_type": "code",
516+
"collapsed": false,
517+
"input": [
518+
"l = [i for i in range(5)]\n",
519+
"print('2 in l,', 2 in l)\n",
520+
"print('3 in l,', 3 in l)\n",
521+
"print('1 in l,', 1 in l) "
522+
],
523+
"language": "python",
524+
"metadata": {},
525+
"outputs": [
526+
{
527+
"output_type": "stream",
528+
"stream": "stdout",
529+
"text": [
530+
"2 in l, True\n",
531+
"3 in l, True\n",
532+
"1 in l, True\n"
533+
]
534+
}
535+
],
536+
"prompt_number": 10
537+
},
469538
{
470539
"cell_type": "code",
471540
"collapsed": false,

0 commit comments

Comments
 (0)