מפגש 15: class, __init__, self, תכונות ומתודות.Session 15: class, __init__, self, attributes and methods.
class, __init__, self, תכונות ומתודות. בסוף המפגש מוסיפים צעד לפרויקט המלווה.class, __init__, self, attributes and methods. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
למהWhy
__init____init__
מתודותMethods
מצבState
הרכבהComposition
תכנוןDesign
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
נושאTopic 1
למהWhy
למה מחלקות?Why classes?
נתונים + פעולות במקום אחדData + actions in one place
מילון עם פונקציות מפוזרות → מחלקהA dict with scattered functions → a class
כל אובייקט עם ערכים משלוEach object has its own values
עד עכשיו ספר היה מילון, והפונקציות שעובדות עליו ישבו בנפרד. מחלקה מאחדת את שניהם: היא מגדירה אילו נתונים יש לכל ספר ומה ספר יודע לעשות. מכל מחלקה יוצרים כמה אובייקטים שרוצים.Until now a book was a dict, and the functions working on it lived elsewhere. A class joins both: it defines what data every book has and what a book can do. From one class you make as many objects as you like.
אנלוגיהAnalogy
מחלקה היא כמו תבנית עוגיות: התבנית אחת, ומכל לחיצה יוצאת עוגייה חדשה. כל עוגייה יכולה לקבל קישוט אחר, אבל הצורה זהה.A class is like a cookie cutter: one cutter, and every press makes a new cookie. Each can be decorated differently, but the shape is the same.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 2
__init____init__
__init__ ו-self__init__ and self
__init__ רץ ביצירה__init__ runs on creation
self.title = title שומר תכונהself.title = title stores an attribute
self = האובייקט הנוכחיself = the current object
הבנאי__init__ רץ אוטומטית כשכותבים Book(...). הפרמטר הראשון, self, הוא האובייקט החדש, וכל self.x = ... יוצר לו תכונה. את self לא מעבירים בעצמנו: פייתון עושה את זה.The constructor (__init__)__init__ runs automatically when you write Book(...). Its first parameter, self, is the new object, and every self.x = ... gives it an attribute. You don't pass self yourself: Python does.
אנלוגיהAnalogy
self הוא כמו המילה "שלי": כשכל ספר אומר "הכותרת שלי", כל אחד מתכוון לכותרת שלו.self is like the word "my": when each book says "my title", each means its own.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
טעות נפוצהCommon mistake
book.py
class Book:def __init__(self, title):
title = title
b = Book("Dune")
print(b.title)
מה Python עונהWhat Python says
AttributeError: 'Book' object has no attribute 'title'
AttributeError: 'Book' object has no attribute 'title': בלי self. זה סתם משתנה מקומי בתוך __init__. כותבים self.title = title.AttributeError: 'Book' object has no attribute 'title': without self. it's just a local variable inside __init__. Write self.title = title.
שוכחים את self. ושומרים במשתנה מקומי שנעלם.Forgetting self. stores into a local variable that vanishes.
נושאTopic 3
מתודותMethods
מתודותMethods
def read(self, n): בתוך המחלקהdef read(self, n): inside the class
קוראים: book.read(50)Call: book.read(50)
מתודה יכולה להחזיר ערך או לשנות את האובייקטA method can return a value or change the object
מתודה היא פונקציה בתוך מחלקה. כשכותבים a.read(50), פייתון קוראת ל-Book.read(a, 50), כלומר a נכנס ל-self. מתודה יכולה לשנות תכונות, להחזיר חישוב, או שניהם.A method is a function inside a class. When you write a.read(50), Python calls Book.read(a, 50), so a goes into self. A method can change attributes, return a calculation, or both.
אנלוגיהAnalogy
מתודות הן כמו הכפתורים של מכשיר: לכל מכשיר כפתורים משלו, והם פועלים רק עליו.Methods are like a device's buttons: each device has its own, and they act only on it.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 4
מצבState
אובייקטים זוכריםObjects remember
כל אובייקט שומר מצב משלוEach object keeps its own state
שינוי אחד לא משפיע על השניChanging one doesn't affect another
השמה = עוד שם לאותו אובייקטAssignment = another name for the same object
כמו רשימות, אובייקטים הם ניתנים לשינוי. c = a לא יוצר עותק: זה עוד שם לאותו אובייקט. בסימולציה רואים את החצים: שני שמות, ספר אחד.Like lists, objects are mutable. c = a makes no copy: it's another name for the same object. In the simulation you can see the arrows: two names, one book.
אנלוגיהAnalogy
שני שמות לאובייקט אחד הם כמו כינוי: "דני" ו"דניאל" הם אותו אדם. אם דני מסתפר, גם דניאל מסתפר.Two names for one object are like a nickname: "Danny" and "Daniel" are the same person. If Danny gets a haircut, so does Daniel.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 5
הרכבהComposition
מחלקה שמחזיקה מחלקותA class that holds classes
Library מחזיקה רשימת BookLibrary holds a list of Book
מתודות של Library קוראות למתודות של BookLibrary's methods call Book's methods
כל מחלקה אחראית על דבר אחדEach class is responsible for one thing
תוכניות אמיתיות בנויות מכמה מחלקות שעובדות יחד. Library מחזיקה רשימה של Book, ויודעת להוסיף, לחפש ולסכם. כל מחלקה דואגת לעניין שלה: הספר יודע על עצמו, הספרייה יודעת על האוסף.Real programs are built from several classes working together. Library holds a list of Book and can add, search and summarise. Each class minds its own business: a book knows about itself, the library about the collection.
אנלוגיהAnalogy
ספרייה ומדפים הם כמו תזמורת ונגנים: כל נגן יודע לנגן בכלי שלו, והמנצח רק מתאם ביניהם.A library and its books are like an orchestra and players: each player plays their instrument, the conductor just coordinates.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
תכנוןDesign
לתכנן מחלקהDesigning a class
שמות עצם → מחלקות ותכונותNouns → classes and attributes
פעלים → מתודותVerbs → methods
שם מחלקה ב-CamelCaseClass names in CamelCase
טריק פשוט לתכנון: כותבים במשפט מה התוכנית עושה. שמות עצם (ספר, ספרייה, כותרת) הופכים למחלקות ולתכונות, ופעלים (להוסיף, לקרוא, לחפש) הופכים למתודות. שמות מחלקות כותבים ב-CamelCase: ReadingGoal, ושמות מתודות ב-snake_case.A simple design trick: write in a sentence what the program does. Nouns (book, library, title) become classes and attributes, verbs (add, read, search) become methods. Class names use CamelCase: ReadingGoal, method names snake_case.
אנלוגיהAnalogy
לתכנן מחלקה זה כמו לתכנן טופס: קודם מחליטים אילו שדות יש (תכונות), ואחר כך אילו כפתורים (מתודות).Designing a class is like designing a form: first decide the fields (attributes), then the buttons (methods).
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נתונים + פעולות במקום אחדData + actions in one place
למה מחלקות?Why classes?
מילון עם פונקציות מפוזרות → מחלקהA dict with scattered functions → a class
למה מחלקות?Why classes?
כל אובייקט עם ערכים משלוEach object has its own values
למה מחלקות?Why classes?
__init__ רץ ביצירה__init__ runs on creation
__init__ ו-self__init__ and self
self.title = title שומר תכונהself.title = title stores an attribute
__init__ ו-self__init__ and self
self = האובייקט הנוכחיself = the current object
__init__ ו-self__init__ and self
def read(self, n): בתוך המחלקהdef read(self, n): inside the class
מתודותMethods
קוראים: book.read(50)Call: book.read(50)
מתודותMethods
מתודה יכולה להחזיר ערך או לשנות את האובייקטA method can return a value or change the object
מתודותMethods
כל אובייקט שומר מצב משלוEach object keeps its own state
אובייקטים זוכריםObjects remember
שינוי אחד לא משפיע על השניChanging one doesn't affect another
אובייקטים זוכריםObjects remember
השמה = עוד שם לאותו אובייקטAssignment = another name for the same object
אובייקטים זוכריםObjects remember
Library מחזיקה רשימת BookLibrary holds a list of Book
מחלקה שמחזיקה מחלקותA class that holds classes
מתודות של Library קוראות למתודות של BookLibrary's methods call Book's methods
מחלקה שמחזיקה מחלקותA class that holds classes
כל מחלקה אחראית על דבר אחדEach class is responsible for one thing
מחלקה שמחזיקה מחלקותA class that holds classes
שמות עצם → מחלקות ותכונותNouns → classes and attributes
לתכנן מחלקהDesigning a class
פעלים → מתודותVerbs → methods
לתכנן מחלקהDesigning a class
מה למדנוWhat we learned
נתונים + פעולות במקום אחדData + actions in one place
__init__ רץ ביצירה__init__ runs on creation
def read(self, n): בתוך המחלקהdef read(self, n): inside the class
כל אובייקט שומר מצב משלוEach object keeps its own state
Library מחזיקה רשימת BookLibrary holds a list of Book
שמות עצם → מחלקות ותכונותNouns → classes and attributes
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.
עכשיו לתרגולNow, practice
5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked