Skip to content

Commit 38092ac

Browse files
committed
mutable objects as default arguments
1 parent e361912 commit 38092ac

2 files changed

Lines changed: 146 additions & 4 deletions

File tree

.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:6f669267e7ec7d07f6bd0ac72d891bdb4881c22c9396efcc19ca5bff94903cf9"
4+
"signature": "sha256:faa74a34746bf250ef2d72e308074083ee5e60789203d70f630f8c67a709e6fe"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -45,7 +45,8 @@
4545
"- [Picking True values from and and or expressions](#false_true_expressions)\n",
4646
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)\n",
4747
"- [Be aware of the consuming generator](#consuming_generator)\n",
48-
"- [`bool` is a subclass of `int`](#bool_int)"
48+
"- [`bool` is a subclass of `int`](#bool_int)\n",
49+
"- [About lambda and closures-in-a-loop pitfall](#lambda_closure)"
4950
]
5051
},
5152
{
@@ -583,6 +584,76 @@
583584
],
584585
"prompt_number": 16
585586
},
587+
{
588+
"cell_type": "markdown",
589+
"metadata": {},
590+
"source": [
591+
"<br>\n",
592+
"<br>\n",
593+
"<a name='lambda_closure'></a>\n",
594+
"\n",
595+
"## About lambda and closures-in-a-loop pitfall\n",
596+
"\n",
597+
"The following example illustrates how the (last) `lambda` is being reused:"
598+
]
599+
},
600+
{
601+
"cell_type": "code",
602+
"collapsed": false,
603+
"input": [
604+
"my_list = [lambda: i for i in range(5)]\n",
605+
"for l in my_list:\n",
606+
" print(l())"
607+
],
608+
"language": "python",
609+
"metadata": {},
610+
"outputs": [
611+
{
612+
"output_type": "stream",
613+
"stream": "stdout",
614+
"text": [
615+
"4\n",
616+
"4\n",
617+
"4\n",
618+
"4\n",
619+
"4\n"
620+
]
621+
}
622+
],
623+
"prompt_number": 24
624+
},
625+
{
626+
"cell_type": "markdown",
627+
"metadata": {},
628+
"source": [
629+
"**Here, a generator can save you some pain:**"
630+
]
631+
},
632+
{
633+
"cell_type": "code",
634+
"collapsed": false,
635+
"input": [
636+
"my_gen = (lambda: n for n in range(5))\n",
637+
"for l in my_gen:\n",
638+
" print(l())"
639+
],
640+
"language": "python",
641+
"metadata": {},
642+
"outputs": [
643+
{
644+
"output_type": "stream",
645+
"stream": "stdout",
646+
"text": [
647+
"0\n",
648+
"1\n",
649+
"2\n",
650+
"3\n",
651+
"4\n"
652+
]
653+
}
654+
],
655+
"prompt_number": 25
656+
},
586657
{
587658
"cell_type": "code",
588659
"collapsed": false,

not_so_obvious_python_stuff.ipynb

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:6f669267e7ec7d07f6bd0ac72d891bdb4881c22c9396efcc19ca5bff94903cf9"
4+
"signature": "sha256:faa74a34746bf250ef2d72e308074083ee5e60789203d70f630f8c67a709e6fe"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -45,7 +45,8 @@
4545
"- [Picking True values from and and or expressions](#false_true_expressions)\n",
4646
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)\n",
4747
"- [Be aware of the consuming generator](#consuming_generator)\n",
48-
"- [`bool` is a subclass of `int`](#bool_int)"
48+
"- [`bool` is a subclass of `int`](#bool_int)\n",
49+
"- [About lambda and closures-in-a-loop pitfall](#lambda_closure)"
4950
]
5051
},
5152
{
@@ -583,6 +584,76 @@
583584
],
584585
"prompt_number": 16
585586
},
587+
{
588+
"cell_type": "markdown",
589+
"metadata": {},
590+
"source": [
591+
"<br>\n",
592+
"<br>\n",
593+
"<a name='lambda_closure'></a>\n",
594+
"\n",
595+
"## About lambda and closures-in-a-loop pitfall\n",
596+
"\n",
597+
"The following example illustrates how the (last) `lambda` is being reused:"
598+
]
599+
},
600+
{
601+
"cell_type": "code",
602+
"collapsed": false,
603+
"input": [
604+
"my_list = [lambda: i for i in range(5)]\n",
605+
"for l in my_list:\n",
606+
" print(l())"
607+
],
608+
"language": "python",
609+
"metadata": {},
610+
"outputs": [
611+
{
612+
"output_type": "stream",
613+
"stream": "stdout",
614+
"text": [
615+
"4\n",
616+
"4\n",
617+
"4\n",
618+
"4\n",
619+
"4\n"
620+
]
621+
}
622+
],
623+
"prompt_number": 24
624+
},
625+
{
626+
"cell_type": "markdown",
627+
"metadata": {},
628+
"source": [
629+
"**Here, a generator can save you some pain:**"
630+
]
631+
},
632+
{
633+
"cell_type": "code",
634+
"collapsed": false,
635+
"input": [
636+
"my_gen = (lambda: n for n in range(5))\n",
637+
"for l in my_gen:\n",
638+
" print(l())"
639+
],
640+
"language": "python",
641+
"metadata": {},
642+
"outputs": [
643+
{
644+
"output_type": "stream",
645+
"stream": "stdout",
646+
"text": [
647+
"0\n",
648+
"1\n",
649+
"2\n",
650+
"3\n",
651+
"4\n"
652+
]
653+
}
654+
],
655+
"prompt_number": 25
656+
},
586657
{
587658
"cell_type": "code",
588659
"collapsed": false,

0 commit comments

Comments
 (0)