对象与类
引入| English | 释义 | 拼音 |
|---|---|---|
| object-oriented programming/ˈɒbdʒekt ˈɔːrɪəntɪd ˈprəʊɡræmɪŋ/ | 面向对象程序设计 | miàn xiàng duì xiàng chéng xù shè jì |
| object/ˈɒbdʒekt/ | 对象 | duì xiàng |
| class/klæs/ | 类 | lèi |
| instance/ˈɪnstəns/ | 实例 | shí lì |
| instance attribute | 实例属性 | shí lì shǔ xìng |
| class attribute | 类属性 | lèi shǔ xìng |
| reference/ˈrefrəns/ | 引用 | yǐn yòng |
| alias/ˈeɪlɪəs/ | 别名 | bié míng |
| equality/iːˈkwɒlɪti/ | 相等 | xiāng děng |
| identity/aɪˈdentɪti/ | 同一 | tóng yī |
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
实例属性来自——
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
init 在构造时运行,为每个实例初始化各自的属性值。
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.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
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.

执行 c = a 之后,哪句成立?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
赋值复制的是引用而不是对象:任一别名处的改动两个名字都看得见。
x = Book('Maps', 5), then y = x, then y.take() runs three times. What is x.copies?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
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.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
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.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
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.
已创建两个 Book 实例;Book.next_id 读到——
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
类属性从 1 起,每构造一次加一,因此现在读到 3。
After the trace, someone runs a.next_id = 9 and then d = Book('Maps', 1). What is d.id?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
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.