diff --git a/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb b/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb index 38b002b..171353f 100644 --- a/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb +++ b/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb @@ -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": [ + "
\n", + "
\n", + "\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, diff --git a/not_so_obvious_python_stuff.ipynb b/not_so_obvious_python_stuff.ipynb index 38b002b..171353f 100644 --- a/not_so_obvious_python_stuff.ipynb +++ b/not_so_obvious_python_stuff.ipynb @@ -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": [ + "
\n", + "
\n", + "\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,