נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
PythonPython

ירושהInheritance

מפגש 16: מחלקת בסיס, super(), דריסת מתודות והרכבה מול ירושה.Session 16: Base classes, super(), overriding methods and composition vs inheritance.

מה נעשה היוםToday's plan

  • ירושהInheritance
  • supersuper
  • דריסהOverride
  • פולימורפיזםPolymorphism
  • עץTree
  • מתיWhen
נושאTopic 1

ירושהInheritance

ירושה בסיסיתBasic inheritance

  • class EBook(Book):class EBook(Book):
  • הבן מקבל הכול מהאבThe child gets everything from the parent
  • ואפשר להוסיף עודAnd can add more

אנלוגיהAnalogy

ירושה היא כמו דגם חדש של רכב: הוא מקבל את כל מה שהיה בדגם הקודם, ומוסיף מסך גדול יותר.Inheritance is like a new car model: it gets everything the previous model had, and adds a bigger screen.
נושאTopic 2

supersuper

super() ו-__init__super() and __init__

  • הבן מוסיף תכונה משלוThe child adds its own attribute
  • super().__init__(...) מפעיל את האבsuper().__init__(...) runs the parent's
  • בלי זה, תכונות האב חסרותWithout it, the parent's attributes are missing

אנלוגיהAnalogy

super() הוא כמו לבנות קומה שנייה: קודם יוצקים את היסודות של הבית הקיים, ורק אז בונים מעליהם.super() is like building a second floor: first pour the existing house's foundation, then build on top.
נושאTopic 3

דריסהOverride

דריסת מתודותOverriding methods

  • אותו שם מתודה בבן = גרסה חדשהSame method name in the child = a new version
  • super().label() משתמש בגרסה של האבsuper().label() reuses the parent's version
  • מוסיפים בלי לשכפלExtend without duplicating

אנלוגיהAnalogy

דריסה היא כמו מתכון משפחתי: הבת משתמשת במתכון של סבתא, אבל מוסיפה תבלין משלה.Overriding is like a family recipe: the daughter uses grandma's recipe but adds her own spice.
נושאTopic 4

פולימורפיזםPolymorphism

פולימורפיזםPolymorphism

  • רשימה של סוגים שוניםA list of different kinds
  • אותה קריאה לכולםThe same call for all
  • כל אחד עונה בדרך שלוEach answers its own way

אנלוגיהAnalogy

פולימורפיזם הוא כמו כפתור "הפעל" אוניברסלי: במכונת כביסה הוא מכבס, במדיח הוא שוטף, אבל אתם לוחצים על אותו כפתור.Polymorphism is like a universal "start" button: on a washing machine it washes, on a dishwasher it rinses, but you press the same button.
נושאTopic 5

עץTree

עץ מחלקות ו-isinstanceClass trees and isinstance

  • אפשר לרשת מבן של בןYou can inherit from a child of a child
  • isinstance בודק גם אבותisinstance checks ancestors too
  • הכול יורש מ-objectEverything inherits from object

אנלוגיהAnalogy

עץ מחלקות הוא כמו עץ משפחה: נכדה היא גם בת של ההורים וגם נכדה של הסבים.A class tree is like a family tree: a granddaughter is both her parents' child and her grandparents' grandchild.
נושאTopic 6

מתיWhen

ירושה או הרכבה?Inheritance or composition?

  • "הוא סוג של" → ירושה"is a kind of" → inheritance
  • "יש לו" → הרכבה"has a" → composition
  • ספק? הרכבהIn doubt? composition

אנלוגיהAnalogy

לרכב יש מנוע, אבל רכב הוא לא סוג של מנוע. לכן הרכב מחזיק מנוע, ולא יורש ממנו.A car has an engine, but a car isn't a kind of engine. So the car holds an engine instead of inheriting from it.

class EBook(Book):class EBook(Book):

הבן מקבל הכול מהאבThe child gets everything from the parent

ואפשר להוסיף עודAnd can add more

הבן מוסיף תכונה משלוThe child adds its own attribute

super().__init__(...) מפעיל את האבsuper().__init__(...) runs the parent's

בלי זה, תכונות האב חסרותWithout it, the parent's attributes are missing

אותו שם מתודה בבן = גרסה חדשהSame method name in the child = a new version

super().label() משתמש בגרסה של האבsuper().label() reuses the parent's version

מוסיפים בלי לשכפלExtend without duplicating

רשימה של סוגים שוניםA list of different kinds

אותה קריאה לכולםThe same call for all

כל אחד עונה בדרך שלוEach answers its own way

אפשר לרשת מבן של בןYou can inherit from a child of a child

isinstance בודק גם אבותisinstance checks ancestors too

הכול יורש מ-objectEverything inherits from object

"הוא סוג של" → ירושה"is a kind of" → inheritance

"יש לו" → הרכבה"has a" → composition

ספק? הרכבהIn doubt? composition

מה למדנוWhat we learned

  • class EBook(Book):class EBook(Book):
  • הבן מוסיף תכונה משלוThe child adds its own attribute
  • אותו שם מתודה בבן = גרסה חדשהSame method name in the child = a new version
  • רשימה של סוגים שוניםA list of different kinds
  • אפשר לרשת מבן של בןYou can inherit from a child of a child
  • "הוא סוג של" → ירושה"is a kind of" → inheritance

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

נבנה על ידיBuilt by Ariel Yohimovich