Skip to content

Commit e361912

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

2 files changed

Lines changed: 100 additions & 4 deletions

File tree

.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:7eaf1c17c09445d2807a0560a73437a138201e67de0b44e98b6c853eff059061"
4+
"signature": "sha256:6f669267e7ec7d07f6bd0ac72d891bdb4881c22c9396efcc19ca5bff94903cf9"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -44,7 +44,8 @@
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",
4646
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)\n",
47-
"- [Be aware of the consuming generator](#consuming_generator)"
47+
"- [Be aware of the consuming generator](#consuming_generator)\n",
48+
"- [`bool` is a subclass of `int`](#bool_int)"
4849
]
4950
},
5051
{
@@ -420,6 +421,13 @@
420421
],
421422
"prompt_number": 9
422423
},
424+
{
425+
"cell_type": "markdown",
426+
"metadata": {},
427+
"source": [
428+
"And a fun fact"
429+
]
430+
},
423431
{
424432
"cell_type": "markdown",
425433
"metadata": {},
@@ -535,6 +543,46 @@
535543
],
536544
"prompt_number": 10
537545
},
546+
{
547+
"cell_type": "markdown",
548+
"metadata": {},
549+
"source": [
550+
"<br>\n",
551+
"<br>\n",
552+
"<a name='bool_int'></a>\n",
553+
"\n",
554+
"## `bool` is a subclass of `int`\n",
555+
"\n",
556+
"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",
557+
"\n",
558+
"Original source: [http://www.peterbe.com/plog/bool-is-int](http://www.peterbe.com/plog/bool-is-int)"
559+
]
560+
},
561+
{
562+
"cell_type": "code",
563+
"collapsed": false,
564+
"input": [
565+
"print('isinstance(True, int):', isinstance(True, int))\n",
566+
"print('True + True:', True + True)\n",
567+
"print('3*True:', 3*True)\n",
568+
"print('3*True - False:', 3*True - False)\n"
569+
],
570+
"language": "python",
571+
"metadata": {},
572+
"outputs": [
573+
{
574+
"output_type": "stream",
575+
"stream": "stdout",
576+
"text": [
577+
"isinstance(True, int): True\n",
578+
"True + True: 2\n",
579+
"3*True: 3\n",
580+
"3*True - False: 3\n"
581+
]
582+
}
583+
],
584+
"prompt_number": 16
585+
},
538586
{
539587
"cell_type": "code",
540588
"collapsed": false,

not_so_obvious_python_stuff.ipynb

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:7eaf1c17c09445d2807a0560a73437a138201e67de0b44e98b6c853eff059061"
4+
"signature": "sha256:6f669267e7ec7d07f6bd0ac72d891bdb4881c22c9396efcc19ca5bff94903cf9"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -44,7 +44,8 @@
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",
4646
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)\n",
47-
"- [Be aware of the consuming generator](#consuming_generator)"
47+
"- [Be aware of the consuming generator](#consuming_generator)\n",
48+
"- [`bool` is a subclass of `int`](#bool_int)"
4849
]
4950
},
5051
{
@@ -420,6 +421,13 @@
420421
],
421422
"prompt_number": 9
422423
},
424+
{
425+
"cell_type": "markdown",
426+
"metadata": {},
427+
"source": [
428+
"And a fun fact"
429+
]
430+
},
423431
{
424432
"cell_type": "markdown",
425433
"metadata": {},
@@ -535,6 +543,46 @@
535543
],
536544
"prompt_number": 10
537545
},
546+
{
547+
"cell_type": "markdown",
548+
"metadata": {},
549+
"source": [
550+
"<br>\n",
551+
"<br>\n",
552+
"<a name='bool_int'></a>\n",
553+
"\n",
554+
"## `bool` is a subclass of `int`\n",
555+
"\n",
556+
"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",
557+
"\n",
558+
"Original source: [http://www.peterbe.com/plog/bool-is-int](http://www.peterbe.com/plog/bool-is-int)"
559+
]
560+
},
561+
{
562+
"cell_type": "code",
563+
"collapsed": false,
564+
"input": [
565+
"print('isinstance(True, int):', isinstance(True, int))\n",
566+
"print('True + True:', True + True)\n",
567+
"print('3*True:', 3*True)\n",
568+
"print('3*True - False:', 3*True - False)\n"
569+
],
570+
"language": "python",
571+
"metadata": {},
572+
"outputs": [
573+
{
574+
"output_type": "stream",
575+
"stream": "stdout",
576+
"text": [
577+
"isinstance(True, int): True\n",
578+
"True + True: 2\n",
579+
"3*True: 3\n",
580+
"3*True - False: 3\n"
581+
]
582+
}
583+
],
584+
"prompt_number": 16
585+
},
538586
{
539587
"cell_type": "code",
540588
"collapsed": false,

0 commit comments

Comments
 (0)