מפגש 16: מחלקת בסיס, super(), דריסת מתודות והרכבה מול ירושה.Session 16: Base classes, super(), overriding methods and composition vs inheritance.
מחלקת בסיס, super(), דריסת מתודות והרכבה מול ירושה. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Base classes, super(), overriding methods and composition vs inheritance. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
ירושהInheritance
supersuper
דריסהOverride
פולימורפיזםPolymorphism
עץTree
מתיWhen
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
נושאTopic 1
ירושהInheritance
ירושה בסיסיתBasic inheritance
class EBook(Book):class EBook(Book):
הבן מקבל הכול מהאבThe child gets everything from the parent
ואפשר להוסיף עודAnd can add more
לפעמים יש כמה סוגים של אותו דבר: ספר מודפס, ספר דיגיטלי, ספר שמע. כולם ספרים, אבל לכל אחד פרט משלו. ירושה מאפשרת לכתוב את המשותף פעם אחת במחלקת האב, וכל מחלקת בן מוסיפה רק את מה שמיוחד לה.Sometimes there are several kinds of the same thing: a printed book, an e-book, an audiobook. All are books, but each has its own detail. Inheritance lets you write what's shared once in the parent class, and each child class adds only what's special to it.
אנלוגיהAnalogy
ירושה היא כמו דגם חדש של רכב: הוא מקבל את כל מה שהיה בדגם הקודם, ומוסיף מסך גדול יותר.Inheritance is like a new car model: it gets everything the previous model had, and adds a bigger screen.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושא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
כשלבן יש __init__ משלו, הוא מחליף את של האב. כדי שהתכונות של האב עדיין ייווצרו, קוראים ל-super(): super().__init__(title, author), ואז מוסיפים את התכונות החדשות.When the child has its own __init__, it replaces the parent's. So the parent's attributes still get created, call super(): super().__init__(title, author), then add the new attributes.
אנלוגיהAnalogy
super() הוא כמו לבנות קומה שנייה: קודם יוצקים את היסודות של הבית הקיים, ורק אז בונים מעליהם.super() is like building a second floor: first pour the existing house's foundation, then build on top.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושא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
דריסה היא כתיבת מתודה בשם זהה לזו של האב. פייתון מחפשת קודם בבן, ואם לא מוצאת, עולה לאב. אפשר להשתמש בתוצאה של האב עם super().label() ולהוסיף עליה.To override is to write a method with the same name as the parent's. Python looks in the child first and, if not found, goes up to the parent. You can reuse the parent's result with super().label() and add to it.
אנלוגיהAnalogy
דריסה היא כמו מתכון משפחתי: הבת משתמשת במתכון של סבתא, אבל מוסיפה תבלין משלה.Overriding is like a family recipe: the daughter uses grandma's recipe but adds her own spice.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 4
פולימורפיזםPolymorphism
פולימורפיזםPolymorphism
רשימה של סוגים שוניםA list of different kinds
אותה קריאה לכולםThe same call for all
כל אחד עונה בדרך שלוEach answers its own way
כוחה האמיתי של ירושה: אפשר לשים ספרים מסוגים שונים באותה רשימה ולקרוא לכולם describe(). הלולאה לא צריכה לדעת מי מה: כל אובייקט יודע לתאר את עצמו. זה פולימורפיזם.Inheritance's real power: put books of different kinds in one list and call describe() on all. The loop doesn't need to know who's what: each object knows how to describe itself. That's polymorphism.
אנלוגיה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.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 5
עץTree
עץ מחלקות ו-isinstanceClass trees and isinstance
אפשר לרשת מבן של בןYou can inherit from a child of a child
ירושה יכולה להיות כמה רמות. isinstance עונה True גם עבור כל אב בשרשרת. ובראש העץ יושבת המחלקה object, שממנה יורשת כל מחלקה בפייתון. __mro__ מראה את סדר החיפוש של פייתון.Inheritance can go several levels. isinstance returns True for every ancestor in the chain too. At the top of the tree sits object, which every Python class inherits from. __mro__ shows Python's search order.
אנלוגיהAnalogy
עץ מחלקות הוא כמו עץ משפחה: נכדה היא גם בת של ההורים וגם נכדה של הסבים.A class tree is like a family tree: a granddaughter is both her parents' child and her grandparents' grandchild.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
מתיWhen
ירושה או הרכבה?Inheritance or composition?
"הוא סוג של" → ירושה"is a kind of" → inheritance
"יש לו" → הרכבה"has a" → composition
ספק? הרכבהIn doubt? composition
כלל אצבע: אם אפשר לומר "X הוא סוג של Y" (ספר שמע הוא סוג של ספר), ירושה מתאימה. אם "ל-X יש Y" (לספרייה יש ספרים), מחזיקים אובייקט בתוך אובייקט: הרכבה. ירושה עמוקה מדי קשה לשינוי, אז כשמתלבטים, מרכיבים.Rule of thumb: if you can say "X is a kind of Y" (an audiobook is a kind of book), inheritance fits. If "X has a Y" (a library has books), keep one object inside another: composition. Deep inheritance is hard to change, so when in doubt, compose.
אנלוגיה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.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
class EBook(Book):class EBook(Book):
ירושה בסיסיתBasic inheritance
הבן מקבל הכול מהאבThe child gets everything from the parent
ירושה בסיסיתBasic inheritance
ואפשר להוסיף עודAnd can add more
ירושה בסיסיתBasic inheritance
הבן מוסיף תכונה משלוThe child adds its own attribute
super() ו-__init__super() and __init__
super().__init__(...) מפעיל את האבsuper().__init__(...) runs the parent's
super() ו-__init__super() and __init__
בלי זה, תכונות האב חסרותWithout it, the parent's attributes are missing
super() ו-__init__super() and __init__
אותו שם מתודה בבן = גרסה חדשהSame method name in the child = a new version
דריסת מתודותOverriding methods
super().label() משתמש בגרסה של האבsuper().label() reuses the parent's version
דריסת מתודותOverriding methods
מוסיפים בלי לשכפלExtend without duplicating
דריסת מתודותOverriding methods
רשימה של סוגים שוניםA list of different kinds
פולימורפיזםPolymorphism
אותה קריאה לכולםThe same call for all
פולימורפיזםPolymorphism
כל אחד עונה בדרך שלוEach answers its own way
פולימורפיזםPolymorphism
אפשר לרשת מבן של בןYou can inherit from a child of a child