Objects and classes
แนะนำใหม่
5:16
การเขียนโปรแกรมเชิงวัตถุ
This course is about how a small script grows into a system that other people can safely change. The first two units build the object: classes, references and…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| object-oriented programming/ˈɒbdʒekt ˈɔːrɪəntɪd ˈprəʊɡræmɪŋ/ | การเขียนโปรแกรมเชิงวัตถุ |
| object/ˈɒbdʒekt/ | ออบเจกต์ |
| class/klæs/ | คลาส |
| instance/ˈɪnstəns/ | อินสแตนซ์ |
| instance attribute/ˈɪnstəns ˈætrɪbjuːt/ | คุณสมบัติของอินสแตนซ์ |
| class attribute/klæs ˈætrɪbjuːt/ | คุณสมบัติของคลาส |
| reference/ˈrefrəns/ | รีเฟอเรนซ์ |
| alias/ˈeɪlɪəs/ | A second name for the same object, so a change made through one name is seen through the other. |
| equality/iːˈkwɒlɪti/ | ความเท่ากัน |
| identity/aɪˈdentɪti/ | อัตลักษณ์ |
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 —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
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.
ความยาก 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.

After c = a, which statement holds?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
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?
ความยาก 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
- Run this, then ask what each name sees:
a = Book('Signals', 3)
b = Book('Signals', 3)
c = a
a.take()
a.take()
a.copiesis 1,b.copiesis 3, andc.copiesis 1, becausecisa: one object with two names.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 —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
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?
ความยาก 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. With
c = aandba separate copy of the same book:
| Test | Asks | a, c |
a, b |
|---|---|---|---|
== |
equal under the class’s comparison? | True |
False |
is |
same object? | True |
False |
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้