Skip to content

Commit b2f2b74

Browse files
committed
W5: Add inheritance example
1 parent 4b5129d commit b2f2b74

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

week5/w5.ipynb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,42 @@
381381
"and avoid repeating code."
382382
]
383383
},
384+
{
385+
"cell_type": "code",
386+
"execution_count": 13,
387+
"metadata": {},
388+
"outputs": [
389+
{
390+
"name": "stdout",
391+
"output_type": "stream",
392+
"text": [
393+
"010101010000111001 code go brrrrrr\n",
394+
"Treating a person...\n"
395+
]
396+
}
397+
],
398+
"source": [
399+
"class ComputerScientist(Person):\n",
400+
" def __init__(self, name, age):\n",
401+
" super().__init__(name, age, \"computer scientist\")\n",
402+
" \n",
403+
" def write_code(self):\n",
404+
" print(\"010101010000111001 code go brrrrrr\")\n",
405+
" \n",
406+
"class Doctor(Person):\n",
407+
" def __init__(self, name, age):\n",
408+
" super().__init__(name, age, \"computer scientist\")\n",
409+
" \n",
410+
" def treat_person(self):\n",
411+
" print(\"Treating a person...\")\n",
412+
"\n",
413+
"donald_knuth = ComputerScientist(\"donald knuth\", 84)\n",
414+
"donald_knuth.write_code()\n",
415+
"\n",
416+
"doctor = Doctor(\"John Doe\", 65)\n",
417+
"doctor.treat_person()"
418+
]
419+
},
384420
{
385421
"cell_type": "markdown",
386422
"metadata": {},

0 commit comments

Comments
 (0)