Skip to content

Commit 0303926

Browse files
committed
W5: Instance methods and small exercise
1 parent 6632ea9 commit 0303926

File tree

1 file changed

+70
-2
lines changed

1 file changed

+70
-2
lines changed

week5/w5.ipynb

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@
215215
},
216216
{
217217
"cell_type": "code",
218-
"execution_count": null,
218+
"execution_count": 3,
219219
"metadata": {},
220220
"outputs": [],
221221
"source": [
@@ -249,7 +249,68 @@
249249
"cell_type": "markdown",
250250
"metadata": {},
251251
"source": [
252-
"**[INSERT diagram for class vs object]**"
252+
"We can also define other \"behaviors\" for each instance of `Person`.\n",
253+
"Let's define an instance method called `introduce` which introduces a new person.\n",
254+
"This is done using the same syntax used to define `__init__`. Again, we must take `self` as the first parameter.\n",
255+
"\n",
256+
"We form a new string which contains the person's name, age, and profession using the same syntax introduced before and print it.\n",
257+
"\n",
258+
"We can then invoke `introduce` by using the `.` syntax and the syntax we used to invoke \"normal\" functions.\n",
259+
"Note that, while `self` is a required parameter for `introduce`, we don't need to explictly pass it to `introduce`."
260+
]
261+
},
262+
{
263+
"cell_type": "code",
264+
"execution_count": null,
265+
"metadata": {},
266+
"outputs": [],
267+
"source": [
268+
"class Person:\n",
269+
" def __init__(self, name, age, profession):\n",
270+
" self.name = name\n",
271+
" self.age = age\n",
272+
" self.profession = profession\n",
273+
"\n",
274+
" def introduce(self):\n",
275+
" print(f\"I'm {self.name}, {self.age} years old. I'm a {self.profession}.\")\n",
276+
"\n",
277+
"my_person = Person(\"tomas\", 20, \"computer scientist\")\n",
278+
"my_person.introduce()"
279+
]
280+
},
281+
{
282+
"cell_type": "markdown",
283+
"metadata": {},
284+
"source": [
285+
"> Task 1: Complete the `eat` method which will allow instances of `Person` to eat.\n",
286+
"\n",
287+
"`eat` should take the food name as a parameter and return \"\\<Name of the person\\> is eating \\<name of the food\\>\""
288+
]
289+
},
290+
{
291+
"cell_type": "code",
292+
"execution_count": null,
293+
"metadata": {},
294+
"outputs": [],
295+
"source": [
296+
"class Person:\n",
297+
" def __init__(self, name, age, profession):\n",
298+
" self.name = name\n",
299+
" self.age = age\n",
300+
" self.profession = profession\n",
301+
"\n",
302+
" def introduce(self):\n",
303+
" print(f\"I'm {self.name}, {self.age} years old. I'm a {self.profession}.\")\n",
304+
" \n",
305+
" def eat(self, food):\n",
306+
" return \"\"\n",
307+
"\n",
308+
"my_person = Person(\"tomas\", 20, \"computer scientist\")\n",
309+
"my_person.introduce()\n",
310+
"\n",
311+
"\n",
312+
"# DO NOT MODIFY\n",
313+
"assert my_person.eat(\"pasta\") == \"tomas is eating pasta\", \"Test failed: Something went wrong, expected 'tomas is eating pasta'\""
253314
]
254315
},
255316
{
@@ -287,6 +348,13 @@
287348
"tomas.describe()"
288349
]
289350
},
351+
{
352+
"cell_type": "markdown",
353+
"metadata": {},
354+
"source": [
355+
"**[INSERT diagram for class vs object]**"
356+
]
357+
},
290358
{
291359
"cell_type": "markdown",
292360
"metadata": {},

0 commit comments

Comments
 (0)