Objects and classes
Diperkenalkan| English | Makna |
|---|---|
| object-oriented programming/ˈɒbdʒekt ˈɔːrɪəntɪd ˈprəʊɡræmɪŋ/ | object-oriented programming |
| object/ˈɒbdʒekt/ | An entity in a program with associated state and behaviour, often an instance of a class. |
| class/klæs/ | A blueprint. |
| instance/ˈɪnstəns/ | A particular occurrence or example; in object-oriented programming, an object created from a class. |
| instance attribute | instance attribute |
| class attribute | class attribute |
| reference/ˈrefrəns/ | reference |
| alias/ˈeɪlɪəs/ | alias |
| equality/iːˈkwɒlɪti/ | equality |
| identity/aɪˈdentɪti/ | identity |
One home for each book
- A library app tracks thousands of books. If titles and copy counts sit in loose variables, one slip updates the wrong book and nobody can tell where.
- Object-oriented programming 面向对象程序设计 gives each book one home: an object that owns its own data and actions.
Classes and instances
- A class 类 is a blueprint. An object 对象 is one thing built from it, also called an instance 实例 of the class.
Bookis the class; the copy of Signals on the shelf is one instance, and Maps is another.
The constructor and self
__init__runs when an object is created and sets up its instance attributes 实例属性. Every method takesself, which is the instance itself.
class Book:
next_id = 1 # shared
def __init__(self, title, n):
self.id = Book.next_id
Book.next_id += 1
self.title = title
self.copies = n
def take(self):
self.copies -= 1
return self.copies
Instance attributes come from —
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
init runs at construction and seeds each instance its own attribute values.
Class attributes are shared
- Class attributes 类属性 live on the class itself, and every instance reads the same one.
Book.next_idbelongs there so every caller agrees on the next number. - Instance attributes such as
titleandcopiesare separate for each object.
A class attribute is copied into each instance when the instance is created.
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
It stays on the class, and every instance reads that one shared value.
Names are references
- Assignment never copies an object. A variable holds a reference 引用 to it, so
c = amakes two aliases 别名 for one object. - A change made through either name is visible through both.

After c = a, which statement holds?
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Assignment copies the reference, not the object: mutation through either alias is visible through both.
x = Book('Maps', 5), then y = x, then y.take() runs three times. What is x.copies?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
x and y are one object, so three takes from 5 leave 2.
Equality and identity
- Equality 相等 (
==) compares values, once a class defines how. Identity 同一 (is) asks whether two names point to the very same object. - If a class does not define
==, Python falls back to identity, so two separate books with the same title are not equal.
Match each term to its meaning.
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
These four words describe every object-oriented program.
Where Python looks for an attribute
obj.xis looked up from the inside out: the instance first, then the class, then the base classes.- So
a.next_id = 9creates an instance attribute that hides the class value foraonly.Book.next_idis untouched.
Put the places Python searches for obj.x in order.
Kesulitan 3/5 · 200 koin + bonus kecil pada penyelesaian pertama hari ini
The first match wins, which is why an instance attribute can hide a class attribute.
Worked example: trace the state
a = Book('Signals', 3),b = Book('Signals', 3),c = a, thena.take()twice. Nowa.copiesis 1,b.copiesis 3, andc.copiesis 1, because c is a.a == bis False,a is cis True, andBook.next_idreads 3: ids 1 and 2 are used.
Two Book instances exist; Book.next_id reads —
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
The class attribute started at 1 and incremented once per construction, so it now reads 3.
After the trace, someone runs a.next_id = 9 and then d = Book('Maps', 1). What is d.id?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
a.next_id = 9 only hides the value for a. Book.next_id was still 3, so d gets id 3.
Three common slips
- Forgetting
selfin a method header or before an attribute name. - Setting a class attribute through an instance. It only hides the shared value for that one object.
- Expecting
d = cto copy. It makes an alias.
Recap
- Class = blueprint, instance = one object;
__init__sets instance attributes throughself. - Class attributes are shared; lookup goes instance, then class, then bases.
- Assignment makes aliases;
istests identity,==tests equality.