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

חזרה למפגשBack
PythonPython

מחלקות ואובייקטיםClasses & objects

מפגש 15: class, __init__, self, תכונות ומתודות.Session 15: class, __init__, self, attributes and methods.

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

  • למהWhy
  • __init____init__
  • מתודותMethods
  • מצבState
  • הרכבהComposition
  • תכנוןDesign
נושאTopic 1

למהWhy

למה מחלקות?Why classes?

  • נתונים + פעולות במקום אחדData + actions in one place
  • מילון עם פונקציות מפוזרות → מחלקהA dict with scattered functions → a class
  • כל אובייקט עם ערכים משלוEach object has its own values

אנלוגיה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.
נושא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

אנלוגיהAnalogy

self הוא כמו המילה "שלי": כשכל ספר אומר "הכותרת שלי", כל אחד מתכוון לכותרת שלו.self is like the word "my": when each book says "my title", each means its own.

טעות נפוצה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.
נושא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

אנלוגיהAnalogy

מתודות הן כמו הכפתורים של מכשיר: לכל מכשיר כפתורים משלו, והם פועלים רק עליו.Methods are like a device's buttons: each device has its own, and they act only on it.
נושאTopic 4

מצבState

אובייקטים זוכריםObjects remember

  • כל אובייקט שומר מצב משלוEach object keeps its own state
  • שינוי אחד לא משפיע על השניChanging one doesn't affect another
  • השמה = עוד שם לאותו אובייקטAssignment = another name for the same object

אנלוגיה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.
נושא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

אנלוגיהAnalogy

ספרייה ומדפים הם כמו תזמורת ונגנים: כל נגן יודע לנגן בכלי שלו, והמנצח רק מתאם ביניהם.A library and its books are like an orchestra and players: each player plays their instrument, the conductor just coordinates.
נושאTopic 6

תכנוןDesign

לתכנן מחלקהDesigning a class

  • שמות עצם → מחלקות ותכונותNouns → classes and attributes
  • פעלים → מתודותVerbs → methods
  • שם מחלקה ב-CamelCaseClass names in CamelCase

אנלוגיהAnalogy

לתכנן מחלקה זה כמו לתכנן טופס: קודם מחליטים אילו שדות יש (תכונות), ואחר כך אילו כפתורים (מתודות).Designing a class is like designing a form: first decide the fields (attributes), then the buttons (methods).

נתונים + פעולות במקום אחדData + actions in one place

מילון עם פונקציות מפוזרות → מחלקהA dict with scattered functions → a class

כל אובייקט עם ערכים משלוEach object has its own values

__init__ רץ ביצירה__init__ runs on creation

self.title = title שומר תכונהself.title = title stores an attribute

self = האובייקט הנוכחיself = the current object

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

כל אובייקט שומר מצב משלוEach object keeps its own state

שינוי אחד לא משפיע על השניChanging one doesn't affect another

השמה = עוד שם לאותו אובייקטAssignment = another name for the same object

Library מחזיקה רשימת BookLibrary holds a list of Book

מתודות של Library קוראות למתודות של BookLibrary's methods call Book's methods

כל מחלקה אחראית על דבר אחדEach class is responsible for one thing

שמות עצם → מחלקות ותכונותNouns → classes and attributes

פעלים → מתודותVerbs → methods

מה למדנו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

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

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

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