mutable objects as default arguments

This commit is contained in:
rasbt 2014-04-15 16:56:51 -04:00
parent 00d6839d17
commit e361912537
2 changed files with 100 additions and 4 deletions

View File

@ -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": [
"<br>\n",
"<br>\n",
"<a name='bool_int'></a>\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,

View File

@ -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": [
"<br>\n",
"<br>\n",
"<a name='bool_int'></a>\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,