Skip to content

Commit def20a6

Browse files
committed
Add polymorphism example
1 parent 37da54f commit def20a6

File tree

1 file changed

+65
-13
lines changed

1 file changed

+65
-13
lines changed

week5/w5.ipynb

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -320,24 +320,15 @@
320320
"are bundled as a single-unit.\n",
321321
"\n",
322322
"As a further example, for **encapsulation**, our class can have other attributes that are not exposed to the \"outside world\".\n",
323-
"For example, we can keep track of the energy of a Person using `_energy` (note the underscore, this represents a private attribute, that is, it shouldn't be used anywhere outside the class), and we use this to decide whether or not a person can walk. The key takeaway for encapsulation is that we only\n",
323+
"For example, we can keep track of the energy of a Person using `_energy` (note the underscore, this represents a protected attribute, that is, it shouldn't be used anywhere outside the class), and we use this to decide whether or not a person can walk. The key takeaway for encapsulation is that we only\n",
324324
"expose what is absolutely necessary to the \"outside world\"."
325325
]
326326
},
327327
{
328328
"cell_type": "code",
329-
"execution_count": 2,
329+
"execution_count": null,
330330
"metadata": {},
331-
"outputs": [
332-
{
333-
"name": "stdout",
334-
"output_type": "stream",
335-
"text": [
336-
"Not enough energy!\n",
337-
"Walking...\n"
338-
]
339-
}
340-
],
331+
"outputs": [],
341332
"source": [
342333
"class Person:\n",
343334
" def __init__(self, name, age, profession):\n",
@@ -519,7 +510,68 @@
519510
{
520511
"cell_type": "markdown",
521512
"metadata": {},
522-
"source": []
513+
"source": [
514+
"Finally, suppose we want to change the introduction of each profession. We can override the `introduce` method for `Doctor` and `Profession`.\n",
515+
"Then, if we have a group of people, for example, a list of instances of `Person`, we can call `introduce` and the correct introduction will be used!\n",
516+
"This is called **polymorphism**: the same code can be used with slightly different objects and behave in a different way."
517+
]
518+
},
519+
{
520+
"cell_type": "code",
521+
"execution_count": 3,
522+
"metadata": {},
523+
"outputs": [
524+
{
525+
"name": "stdout",
526+
"output_type": "stream",
527+
"text": [
528+
"I'm a computer scientist!!!111!!\n",
529+
"I'm Person, 25 years old. I'm a phd.\n",
530+
"I'm Person2, 30 years old. I'm a mechanic.\n",
531+
"I'm a doctor!!!111!!\n"
532+
]
533+
}
534+
],
535+
"source": [
536+
"class ComputerScientist(Person):\n",
537+
" def __init__(self, name, age):\n",
538+
" \"\"\"\n",
539+
" We're invoking the constructor of Person,\n",
540+
" but we set the profession to be \"computer scientist\"\n",
541+
" \"\"\"\n",
542+
" super().__init__(name, age, \"computer scientist\")\n",
543+
" \n",
544+
" def write_code(self):\n",
545+
" print(\"010101010000111001 code go brrrrrr\")\n",
546+
"\n",
547+
" def introduce(self):\n",
548+
" print(\"I'm a computer scientist!!!111!!\")\n",
549+
" \n",
550+
"class Doctor(Person):\n",
551+
" def __init__(self, name, age):\n",
552+
" \"\"\"\n",
553+
" We're invoking the constructor of Person,\n",
554+
" but we set the profession to be \"doctor\"\n",
555+
" \"\"\"\n",
556+
" super().__init__(name, age, \"doctor\")\n",
557+
" \n",
558+
" def treat_person(self):\n",
559+
" print(\"Treating a person...\")\n",
560+
"\n",
561+
" def introduce(self):\n",
562+
" print(\"I'm a doctor!!!111!!\")\n",
563+
"\n",
564+
"\n",
565+
"donald_knuth = ComputerScientist(\"donald knuth\", 84)\n",
566+
"person = Person(\"Person\", 25, \"phd\")\n",
567+
"person_2 = Person(\"Person2\", 30, \"mechanic\")\n",
568+
"doctor = Doctor(\"John Doe\", 65)\n",
569+
"\n",
570+
"people = [donald_knuth, person, person_2, doctor]\n",
571+
"\n",
572+
"for p in people:\n",
573+
" p.introduce()"
574+
]
523575
},
524576
{
525577
"cell_type": "markdown",

0 commit comments

Comments
 (0)