Skip to content

Commit 9e3afc5

Browse files
committed
class fix
1 parent 38e5d11 commit 9e3afc5

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:e20426bb66111696c7ec4f8d754946061e129b3e332a6cc2fd00331b9540d8c5"
4+
"signature": "sha256:a9475a6f1134f87066829ecff7dab70b1fab2b2cc8ae3ee110b3e719b9626c23"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -117,6 +117,60 @@
117117
],
118118
"prompt_number": 2
119119
},
120+
{
121+
"cell_type": "markdown",
122+
"metadata": {},
123+
"source": [
124+
"So what actually happened above was that class `C` was looking in the parent class `A` for the method `.foo()` first (and found it)!"
125+
]
126+
},
127+
{
128+
"cell_type": "markdown",
129+
"metadata": {},
130+
"source": [
131+
"I received an email with a nice suggestion using a more nested example to illustrate Guido van Rossum's point a little bit better:"
132+
]
133+
},
134+
{
135+
"cell_type": "code",
136+
"collapsed": false,
137+
"input": [
138+
"class A(object):\n",
139+
" def foo(self):\n",
140+
" print(\"class A\")\n",
141+
"\n",
142+
"class B(A):\n",
143+
" pass\n",
144+
"\n",
145+
"class C(A):\n",
146+
" def foo(self):\n",
147+
" print(\"class C\")\n",
148+
"\n",
149+
"class D(B,C):\n",
150+
" pass\n",
151+
"\n",
152+
"D().foo()"
153+
],
154+
"language": "python",
155+
"metadata": {},
156+
"outputs": [
157+
{
158+
"output_type": "stream",
159+
"stream": "stdout",
160+
"text": [
161+
"class C\n"
162+
]
163+
}
164+
],
165+
"prompt_number": 3
166+
},
167+
{
168+
"cell_type": "markdown",
169+
"metadata": {},
170+
"source": [
171+
"Here, class `D` searches in `B` first, which in turn inherits from `A` (note that class `C` also inherits from `A`, but has its own `.foo()` method) so that we come up with the search order: `D, B, C, A`"
172+
]
173+
},
120174
{
121175
"cell_type": "markdown",
122176
"metadata": {},

not_so_obvious_python_stuff.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:a6418912c66fa2e2c0c8b76a1ba6cb0cb8d33c37c8fd64338d77898af4589286"
4+
"signature": "sha256:a9475a6f1134f87066829ecff7dab70b1fab2b2cc8ae3ee110b3e719b9626c23"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -168,7 +168,7 @@
168168
"cell_type": "markdown",
169169
"metadata": {},
170170
"source": [
171-
"Here, class `D` searches in `B` first, which in turn inherits from `A` (note that class `C` also inherits from `A`, but has its own `.foo()` method) so that we come up with the search order: `D, B, A, C`"
171+
"Here, class `D` searches in `B` first, which in turn inherits from `A` (note that class `C` also inherits from `A`, but has its own `.foo()` method) so that we come up with the search order: `D, B, C, A`"
172172
]
173173
},
174174
{

0 commit comments

Comments
 (0)