Skip to content

Commit 95227cb

Browse files
committed
types as variables
1 parent c561c92 commit 95227cb

2 files changed

Lines changed: 174 additions & 28 deletions

File tree

.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:d8ba69c66769cf62e5201b70ed7d717913017f6f09492848ce164b50068bd2ba"
4+
"signature": "sha256:5e808c95d2003c791e04a26d378bc816b2010a59171244a18d73487060d9e029"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -79,7 +79,8 @@
7979
"- [Creating copies of mutable objects](#copy_mutable)\n",
8080
"- [Key differences between Python 2 and 3](#python_differences)\n",
8181
"- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n",
82-
"- [Abortive statements in `finally` blocks](#finally_blocks)"
82+
"- [Abortive statements in `finally` blocks](#finally_blocks)\n",
83+
"- [Assigning types to variables as values](#variable_types)"
8384
]
8485
},
8586
{
@@ -922,7 +923,7 @@
922923
"cell_type": "markdown",
923924
"metadata": {},
924925
"source": [
925-
"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",
926+
"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). In order to avoid syntax errors in old (but perfectly working) Python code, `bool` was added as a subclass of `int` in Python 2.3.\n",
926927
"\n",
927928
"Original source: [http://www.peterbe.com/plog/bool-is-int](http://www.peterbe.com/plog/bool-is-int)"
928929
]
@@ -933,7 +934,7 @@
933934
"input": [
934935
"print('isinstance(True, int):', isinstance(True, int))\n",
935936
"print('True + True:', True + True)\n",
936-
"print('3*True:', 3*True)\n",
937+
"print('3*True + True:', 3*True + True)\n",
937938
"print('3*True - False:', 3*True - False)\n"
938939
],
939940
"language": "python",
@@ -945,12 +946,12 @@
945946
"text": [
946947
"isinstance(True, int): True\n",
947948
"True + True: 2\n",
948-
"3*True: 3\n",
949+
"3*True + True: 4\n",
949950
"3*True - False: 3\n"
950951
]
951952
}
952953
],
953-
"prompt_number": 16
954+
"prompt_number": 28
954955
},
955956
{
956957
"cell_type": "markdown",
@@ -979,11 +980,11 @@
979980
"cell_type": "markdown",
980981
"metadata": {},
981982
"source": [
982-
"Remember the [\"consuming generators\"](consuming_generators)? This example is somewhat related, but the result might still come unexpected. \n",
983+
"Remember the section about the [\"consuming generators\"](consuming_generators)? This example is somewhat related, but the result might still come unexpected. \n",
983984
"\n",
984985
"(Original source: [http://openhome.cc/eGossip/Blog/UnderstandingLambdaClosure3.html](http://openhome.cc/eGossip/Blog/UnderstandingLambdaClosure3.html))\n",
985986
"\n",
986-
"In the first example below, where we call a `lambda` function in a list comprehension, the value `i` is dereferenced every time we call `lambda` within the scope of the list comprehension. Since the list is already constructed when we `for-loop` through the list, it is set to the last value 4."
987+
"In the first example below, we call a `lambda` function in a list comprehension, and the value `i` will be dereferenced every time we call `lambda` within the scope of the list comprehension. Since the list is already constructed when we `for-loop` through the list, it will be set to the last value 4."
987988
]
988989
},
989990
{
@@ -1009,7 +1010,7 @@
10091010
]
10101011
}
10111012
],
1012-
"prompt_number": 11
1013+
"prompt_number": 29
10131014
},
10141015
{
10151016
"cell_type": "markdown",
@@ -1041,7 +1042,7 @@
10411042
]
10421043
}
10431044
],
1044-
"prompt_number": 9
1045+
"prompt_number": 30
10451046
},
10461047
{
10471048
"cell_type": "markdown",
@@ -1106,7 +1107,7 @@
11061107
]
11071108
}
11081109
],
1109-
"prompt_number": 33
1110+
"prompt_number": 31
11101111
},
11111112
{
11121113
"cell_type": "markdown",
@@ -1484,8 +1485,8 @@
14841485
"cell_type": "markdown",
14851486
"metadata": {},
14861487
"source": [
1487-
"Not, really (or significantly, see the benchmarks below). So what's the reason to prefer one over the other?\n",
1488-
"- use lists if you want to use list methods \n",
1488+
"\"List comprehensions are fast, but generators are faster!?\" - No, not really (or significantly, see the benchmarks below). So what's the reason to prefer one over the other?\n",
1489+
"- use lists if you want to use the plethora of list methods \n",
14891490
"- use generators when you are dealing with huge collections to avoid memory issues"
14901491
]
14911492
},
@@ -1699,7 +1700,7 @@
16991700
"cell_type": "markdown",
17001701
"metadata": {},
17011702
"source": [
1702-
"It can be really dangerous to modify a list when iterating through - it is a very common pitfall that can cause unintended behavior! \n",
1703+
"It can be really dangerous to modify a list when iterating through it - this is a very common pitfall that can cause unintended behavior! \n",
17031704
"Look at the following examples, and for a fun exercise: try to figure out what is going on before you skip to the solution!"
17041705
]
17051706
},
@@ -2865,6 +2866,78 @@
28652866
"Here, the abortive `return` statement in the `finally` block simply overrules the `return` in the `try` block, since **`finally` is guaranteed to always be executed.** So, be careful using abortive statements in `finally` blocks!"
28662867
]
28672868
},
2869+
{
2870+
"cell_type": "markdown",
2871+
"metadata": {},
2872+
"source": [
2873+
"<br>\n",
2874+
"<br>\n",
2875+
"<a name=\"variable_types\">"
2876+
]
2877+
},
2878+
{
2879+
"cell_type": "markdown",
2880+
"metadata": {},
2881+
"source": [
2882+
"#Assigning types to variables as values"
2883+
]
2884+
},
2885+
{
2886+
"cell_type": "markdown",
2887+
"metadata": {},
2888+
"source": [
2889+
"I am not yet sure in which context this can be useful, but it is a nice fun fact to know that we can assign types as values to variables."
2890+
]
2891+
},
2892+
{
2893+
"cell_type": "code",
2894+
"collapsed": false,
2895+
"input": [
2896+
"a_var = str\n",
2897+
"a_var(123)"
2898+
],
2899+
"language": "python",
2900+
"metadata": {},
2901+
"outputs": [
2902+
{
2903+
"metadata": {},
2904+
"output_type": "pyout",
2905+
"prompt_number": 1,
2906+
"text": [
2907+
"'123'"
2908+
]
2909+
}
2910+
],
2911+
"prompt_number": 1
2912+
},
2913+
{
2914+
"cell_type": "code",
2915+
"collapsed": false,
2916+
"input": [
2917+
"from random import choice\n",
2918+
"\n",
2919+
"a, b, c = float, int, str\n",
2920+
"for i in range(5):\n",
2921+
" j = choice([a,b,c])(i)\n",
2922+
" print(j, type(j))"
2923+
],
2924+
"language": "python",
2925+
"metadata": {},
2926+
"outputs": [
2927+
{
2928+
"output_type": "stream",
2929+
"stream": "stdout",
2930+
"text": [
2931+
"0 <class 'int'>\n",
2932+
"1 <class 'int'>\n",
2933+
"2.0 <class 'float'>\n",
2934+
"3 <class 'str'>\n",
2935+
"4 <class 'int'>\n"
2936+
]
2937+
}
2938+
],
2939+
"prompt_number": 4
2940+
},
28682941
{
28692942
"cell_type": "code",
28702943
"collapsed": false,

not_so_obvious_python_stuff.ipynb

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:d8ba69c66769cf62e5201b70ed7d717913017f6f09492848ce164b50068bd2ba"
4+
"signature": "sha256:5e808c95d2003c791e04a26d378bc816b2010a59171244a18d73487060d9e029"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -79,7 +79,8 @@
7979
"- [Creating copies of mutable objects](#copy_mutable)\n",
8080
"- [Key differences between Python 2 and 3](#python_differences)\n",
8181
"- [Function annotations - What are those `->`'s in my Python code?](#function_annotation)\n",
82-
"- [Abortive statements in `finally` blocks](#finally_blocks)"
82+
"- [Abortive statements in `finally` blocks](#finally_blocks)\n",
83+
"- [Assigning types to variables as values](#variable_types)"
8384
]
8485
},
8586
{
@@ -922,7 +923,7 @@
922923
"cell_type": "markdown",
923924
"metadata": {},
924925
"source": [
925-
"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",
926+
"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). In order to avoid syntax errors in old (but perfectly working) Python code, `bool` was added as a subclass of `int` in Python 2.3.\n",
926927
"\n",
927928
"Original source: [http://www.peterbe.com/plog/bool-is-int](http://www.peterbe.com/plog/bool-is-int)"
928929
]
@@ -933,7 +934,7 @@
933934
"input": [
934935
"print('isinstance(True, int):', isinstance(True, int))\n",
935936
"print('True + True:', True + True)\n",
936-
"print('3*True:', 3*True)\n",
937+
"print('3*True + True:', 3*True + True)\n",
937938
"print('3*True - False:', 3*True - False)\n"
938939
],
939940
"language": "python",
@@ -945,12 +946,12 @@
945946
"text": [
946947
"isinstance(True, int): True\n",
947948
"True + True: 2\n",
948-
"3*True: 3\n",
949+
"3*True + True: 4\n",
949950
"3*True - False: 3\n"
950951
]
951952
}
952953
],
953-
"prompt_number": 16
954+
"prompt_number": 28
954955
},
955956
{
956957
"cell_type": "markdown",
@@ -979,11 +980,11 @@
979980
"cell_type": "markdown",
980981
"metadata": {},
981982
"source": [
982-
"Remember the [\"consuming generators\"](consuming_generators)? This example is somewhat related, but the result might still come unexpected. \n",
983+
"Remember the section about the [\"consuming generators\"](consuming_generators)? This example is somewhat related, but the result might still come unexpected. \n",
983984
"\n",
984985
"(Original source: [http://openhome.cc/eGossip/Blog/UnderstandingLambdaClosure3.html](http://openhome.cc/eGossip/Blog/UnderstandingLambdaClosure3.html))\n",
985986
"\n",
986-
"In the first example below, where we call a `lambda` function in a list comprehension, the value `i` is dereferenced every time we call `lambda` within the scope of the list comprehension. Since the list is already constructed when we `for-loop` through the list, it is set to the last value 4."
987+
"In the first example below, we call a `lambda` function in a list comprehension, and the value `i` will be dereferenced every time we call `lambda` within the scope of the list comprehension. Since the list is already constructed when we `for-loop` through the list, it will be set to the last value 4."
987988
]
988989
},
989990
{
@@ -1009,7 +1010,7 @@
10091010
]
10101011
}
10111012
],
1012-
"prompt_number": 11
1013+
"prompt_number": 29
10131014
},
10141015
{
10151016
"cell_type": "markdown",
@@ -1041,7 +1042,7 @@
10411042
]
10421043
}
10431044
],
1044-
"prompt_number": 9
1045+
"prompt_number": 30
10451046
},
10461047
{
10471048
"cell_type": "markdown",
@@ -1106,7 +1107,7 @@
11061107
]
11071108
}
11081109
],
1109-
"prompt_number": 33
1110+
"prompt_number": 31
11101111
},
11111112
{
11121113
"cell_type": "markdown",
@@ -1484,8 +1485,8 @@
14841485
"cell_type": "markdown",
14851486
"metadata": {},
14861487
"source": [
1487-
"Not, really (or significantly, see the benchmarks below). So what's the reason to prefer one over the other?\n",
1488-
"- use lists if you want to use list methods \n",
1488+
"\"List comprehensions are fast, but generators are faster!?\" - No, not really (or significantly, see the benchmarks below). So what's the reason to prefer one over the other?\n",
1489+
"- use lists if you want to use the plethora of list methods \n",
14891490
"- use generators when you are dealing with huge collections to avoid memory issues"
14901491
]
14911492
},
@@ -1699,7 +1700,7 @@
16991700
"cell_type": "markdown",
17001701
"metadata": {},
17011702
"source": [
1702-
"It can be really dangerous to modify a list when iterating through - it is a very common pitfall that can cause unintended behavior! \n",
1703+
"It can be really dangerous to modify a list when iterating through it - this is a very common pitfall that can cause unintended behavior! \n",
17031704
"Look at the following examples, and for a fun exercise: try to figure out what is going on before you skip to the solution!"
17041705
]
17051706
},
@@ -2865,6 +2866,78 @@
28652866
"Here, the abortive `return` statement in the `finally` block simply overrules the `return` in the `try` block, since **`finally` is guaranteed to always be executed.** So, be careful using abortive statements in `finally` blocks!"
28662867
]
28672868
},
2869+
{
2870+
"cell_type": "markdown",
2871+
"metadata": {},
2872+
"source": [
2873+
"<br>\n",
2874+
"<br>\n",
2875+
"<a name=\"variable_types\">"
2876+
]
2877+
},
2878+
{
2879+
"cell_type": "markdown",
2880+
"metadata": {},
2881+
"source": [
2882+
"#Assigning types to variables as values"
2883+
]
2884+
},
2885+
{
2886+
"cell_type": "markdown",
2887+
"metadata": {},
2888+
"source": [
2889+
"I am not yet sure in which context this can be useful, but it is a nice fun fact to know that we can assign types as values to variables."
2890+
]
2891+
},
2892+
{
2893+
"cell_type": "code",
2894+
"collapsed": false,
2895+
"input": [
2896+
"a_var = str\n",
2897+
"a_var(123)"
2898+
],
2899+
"language": "python",
2900+
"metadata": {},
2901+
"outputs": [
2902+
{
2903+
"metadata": {},
2904+
"output_type": "pyout",
2905+
"prompt_number": 1,
2906+
"text": [
2907+
"'123'"
2908+
]
2909+
}
2910+
],
2911+
"prompt_number": 1
2912+
},
2913+
{
2914+
"cell_type": "code",
2915+
"collapsed": false,
2916+
"input": [
2917+
"from random import choice\n",
2918+
"\n",
2919+
"a, b, c = float, int, str\n",
2920+
"for i in range(5):\n",
2921+
" j = choice([a,b,c])(i)\n",
2922+
" print(j, type(j))"
2923+
],
2924+
"language": "python",
2925+
"metadata": {},
2926+
"outputs": [
2927+
{
2928+
"output_type": "stream",
2929+
"stream": "stdout",
2930+
"text": [
2931+
"0 <class 'int'>\n",
2932+
"1 <class 'int'>\n",
2933+
"2.0 <class 'float'>\n",
2934+
"3 <class 'str'>\n",
2935+
"4 <class 'int'>\n"
2936+
]
2937+
}
2938+
],
2939+
"prompt_number": 4
2940+
},
28682941
{
28692942
"cell_type": "code",
28702943
"collapsed": false,

0 commit comments

Comments
 (0)