You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: notebooks/beginner/classes.ipynb
+132Lines changed: 132 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -141,6 +141,138 @@
141
141
"inst1.show_info()\n",
142
142
"inst2.show_info()"
143
143
]
144
+
},
145
+
{
146
+
"cell_type": "markdown",
147
+
"metadata": {},
148
+
"source": [
149
+
"## Public vs private\n",
150
+
"In python there's now strict separation for private/public methods or instance variables. The convention is to start the name of the method or instance variable with underscore if it should be treated as private. Private means that it should not be accessed from outside of the class.\n",
151
+
"\n",
152
+
"For example, let's consider that we have a `Person` class which has `age` as an instance variable. We want that `age` is not directly accessed (e.g. changed) after the instance is created. In Python, this would be:"
153
+
]
154
+
},
155
+
{
156
+
"cell_type": "code",
157
+
"execution_count": null,
158
+
"metadata": {},
159
+
"outputs": [],
160
+
"source": [
161
+
"class Person:\n",
162
+
" def __init__(self, age):\n",
163
+
" self._age = age\n",
164
+
"\n",
165
+
"example_person = Person(age=15)\n",
166
+
"# You can't do this:\n",
167
+
"# print(example_person.age)\n",
168
+
"# Nor this:\n",
169
+
"# example_person.age = 16"
170
+
]
171
+
},
172
+
{
173
+
"cell_type": "markdown",
174
+
"metadata": {},
175
+
"source": [
176
+
"If you want the `age` to be readable but not writable, you can use `property`:"
177
+
]
178
+
},
179
+
{
180
+
"cell_type": "code",
181
+
"execution_count": null,
182
+
"metadata": {},
183
+
"outputs": [],
184
+
"source": [
185
+
"class Person:\n",
186
+
" def __init__(self, age):\n",
187
+
" self._age = age\n",
188
+
"\n",
189
+
" @property\n",
190
+
" def age(self):\n",
191
+
" return self._age\n",
192
+
"\n",
193
+
"example_person = Person(age=15)\n",
194
+
"# Now you can do this:\n",
195
+
"print(example_person.age)\n",
196
+
"# But not this:\n",
197
+
"#example_person.age = 16"
198
+
]
199
+
},
200
+
{
201
+
"cell_type": "markdown",
202
+
"metadata": {},
203
+
"source": [
204
+
"This way you can have a controlled access to the instance variables of your class: "
205
+
]
206
+
},
207
+
{
208
+
"cell_type": "code",
209
+
"execution_count": null,
210
+
"metadata": {},
211
+
"outputs": [],
212
+
"source": [
213
+
"class Person:\n",
214
+
" def __init__(self, age):\n",
215
+
" self._age = age\n",
216
+
"\n",
217
+
" @property\n",
218
+
" def age(self):\n",
219
+
" return self._age\n",
220
+
"\n",
221
+
" def celebrate_birthday(self):\n",
222
+
" self._age += 1\n",
223
+
" print('Happy bday for {} years old!'.format(self._age))\n",
224
+
"\n",
225
+
"example_person = Person(age=15)\n",
226
+
"example_person.celebrate_birthday()"
227
+
]
228
+
},
229
+
{
230
+
"cell_type": "markdown",
231
+
"metadata": {},
232
+
"source": [
233
+
"## Introduction to inheritance"
234
+
]
235
+
},
236
+
{
237
+
"cell_type": "code",
238
+
"execution_count": null,
239
+
"metadata": {},
240
+
"outputs": [],
241
+
"source": [
242
+
"class Animal:\n",
243
+
" def greet(self):\n",
244
+
" print('Hello, I am an animal')\n",
245
+
"\n",
246
+
" @property\n",
247
+
" def favorite_food(self):\n",
248
+
" return 'beef'\n",
249
+
"\n",
250
+
"\n",
251
+
"class Dog(Animal):\n",
252
+
" def greet(self):\n",
253
+
" print('wof wof')\n",
254
+
"\n",
255
+
"\n",
256
+
"class Cat(Animal):\n",
257
+
" @property\n",
258
+
" def favorite_food(self):\n",
259
+
" return 'fish'"
260
+
]
261
+
},
262
+
{
263
+
"cell_type": "code",
264
+
"execution_count": null,
265
+
"metadata": {},
266
+
"outputs": [],
267
+
"source": [
268
+
"dog = Dog()\n",
269
+
"dog.greet()\n",
270
+
"print(\"Dog's favorite food is {}\".format(dog.favorite_food))\n",
271
+
"\n",
272
+
"cat = Cat()\n",
273
+
"cat.greet()\n",
274
+
"print(\"Cat's favorite food is {}\".format(cat.favorite_food))"
<h2id="Public-vs-private">Public vs private<aclass="anchor-link" href="#Public-vs-private">¶</a></h2><p>In python there's now strict separation for private/public methods or instance variables. The convention is to start the name of the method or instance variable with underscore if it should be treated as private. Private means that it should not be accessed from outside of the class.</p>
12070
+
<p>For example, let's consider that we have a <code>Person</code> class which has <code>age</code> as an instance variable. We want that <code>age</code> is not directly accessed (e.g. changed) after the instance is created. In Python, this would be:</p>
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="s1">'Happy bday for </span><spanclass="si">{}</span><spanclass="s1"> years old!'</span><spanclass="o">.</span><spanclass="n">format</span><spanclass="p">(</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">_age</span><spanclass="p">))</span>
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="s2">"Dog's favorite food is </span><spanclass="si">{}</span><spanclass="s2">"</span><spanclass="o">.</span><spanclass="n">format</span><spanclass="p">(</span><spanclass="n">dog</span><spanclass="o">.</span><spanclass="n">favorite_food</span><spanclass="p">))</span>
<spanclass="nb">print</span><spanclass="p">(</span><spanclass="s2">"Cat's favorite food is </span><spanclass="si">{}</span><spanclass="s2">"</span><spanclass="o">.</span><spanclass="n">format</span><spanclass="p">(</span><spanclass="n">cat</span><spanclass="o">.</span><spanclass="n">favorite_food</span><spanclass="p">))</span>
0 commit comments