From e3619125379a92fb3bb4b6e0bc590f376551cacb Mon Sep 17 00:00:00 2001 From: rasbt Date: Tue, 15 Apr 2014 16:56:51 -0400 Subject: [PATCH] mutable objects as default arguments --- ...t_so_obvious_python_stuff-checkpoint.ipynb | 52 ++++++++++++++++++- not_so_obvious_python_stuff.ipynb | 52 ++++++++++++++++++- 2 files changed, 100 insertions(+), 4 deletions(-) diff --git a/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb b/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb index 890f3fd..38b002b 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:7eaf1c17c09445d2807a0560a73437a138201e67de0b44e98b6c853eff059061" + "signature": "sha256:6f669267e7ec7d07f6bd0ac72d891bdb4881c22c9396efcc19ca5bff94903cf9" }, "nbformat": 3, "nbformat_minor": 0, @@ -44,7 +44,8 @@ "- [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)\n", - "- [Be aware of the consuming generator](#consuming_generator)" + "- [Be aware of the consuming generator](#consuming_generator)\n", + "- [`bool` is a subclass of `int`](#bool_int)" ] }, { @@ -420,6 +421,13 @@ ], "prompt_number": 9 }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And a fun fact" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -535,6 +543,46 @@ ], "prompt_number": 10 }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
\n", + "\n", + "\n", + "## `bool` is a subclass of `int`\n", + "\n", + "Chicken or egg? In the history of Python (Python 2.2 to be specific) truth values were implemented via 1 and 0 (similar to the old C), to avoid syntax error in old (but perfectly working) code, `bool` was added as a subclass of `int` in Python 2.3.\n", + "\n", + "Original source: [http://www.peterbe.com/plog/bool-is-int](http://www.peterbe.com/plog/bool-is-int)" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print('isinstance(True, int):', isinstance(True, int))\n", + "print('True + True:', True + True)\n", + "print('3*True:', 3*True)\n", + "print('3*True - False:', 3*True - False)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "isinstance(True, int): True\n", + "True + True: 2\n", + "3*True: 3\n", + "3*True - False: 3\n" + ] + } + ], + "prompt_number": 16 + }, { "cell_type": "code", "collapsed": false, diff --git a/not_so_obvious_python_stuff.ipynb b/not_so_obvious_python_stuff.ipynb index 890f3fd..38b002b 100644 --- a/not_so_obvious_python_stuff.ipynb +++ b/not_so_obvious_python_stuff.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:7eaf1c17c09445d2807a0560a73437a138201e67de0b44e98b6c853eff059061" + "signature": "sha256:6f669267e7ec7d07f6bd0ac72d891bdb4881c22c9396efcc19ca5bff94903cf9" }, "nbformat": 3, "nbformat_minor": 0, @@ -44,7 +44,8 @@ "- [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)\n", - "- [Be aware of the consuming generator](#consuming_generator)" + "- [Be aware of the consuming generator](#consuming_generator)\n", + "- [`bool` is a subclass of `int`](#bool_int)" ] }, { @@ -420,6 +421,13 @@ ], "prompt_number": 9 }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And a fun fact" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -535,6 +543,46 @@ ], "prompt_number": 10 }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
\n", + "\n", + "\n", + "## `bool` is a subclass of `int`\n", + "\n", + "Chicken or egg? In the history of Python (Python 2.2 to be specific) truth values were implemented via 1 and 0 (similar to the old C), to avoid syntax error in old (but perfectly working) code, `bool` was added as a subclass of `int` in Python 2.3.\n", + "\n", + "Original source: [http://www.peterbe.com/plog/bool-is-int](http://www.peterbe.com/plog/bool-is-int)" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print('isinstance(True, int):', isinstance(True, int))\n", + "print('True + True:', True + True)\n", + "print('3*True:', 3*True)\n", + "print('3*True - False:', 3*True - False)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "isinstance(True, int): True\n", + "True + True: 2\n", + "3*True: 3\n", + "3*True - False: 3\n" + ] + } + ], + "prompt_number": 16 + }, { "cell_type": "code", "collapsed": false,