Objects and classes
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A class is a blueprint and objects are the things you build from it, each with its own data. Step through a small BankAccount class in Python: build two accounts with __init__, call methods on them, see why the balance is changed only through methods, then make a Savings subclass that reuses and overrides code, and a Bank that has accounts instead of being one.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01After x = BankAccount("Xia") and y = BankAccount("Yan") , then x.deposit(20) , what is y._balance?
x and y are two separate objects built from the same class. Each has its own _balance, so depositing into x leaves y at 0.
02Inside def deposit(self, amount): , what is self when you call x.deposit(20) ?
x.deposit(20) is the same as BankAccount.deposit(x, 20): Python passes the object the method was called on as the first argument, which is named self.
03Savings inherits from BankAccount and defines only withdraw. What runs for s.deposit(30) ?
When a method is not found on the subclass, Python looks in its parent class. A subclass reuses every method it does not override.
04for acc in (a, s): acc.withdraw(5) takes 5 from a but 6 from s. What is this called?
Polymorphism: the same method name works on objects of different classes, and each object runs its own class's version. Savings' withdraw adds a fee of 1.
05A Car needs an Engine. Which design fits best?
A car is not an engine, it has one. That is a has-a relationship, so composition (an attribute holding an Engine object) fits; keep inheritance for a true is-a.
المسرد
- Class
- A blueprint, written with class Name:, that describes what its objects hold and what they can do.
- Object (instance)
- One thing built from a class, with its own attributes (data), such as a.owner and a._balance.
- Method and self
- A function defined in a class. Calling a.deposit(50) runs it with self set to the object a.
- __init__
- The method Python runs on every new object right after making it, to give it its starting attributes.
- Encapsulation
- Keeping an object's data behind its methods, which check the rules. In Python a leading underscore, as in _balance, marks data as internal.
- Inheritance and overriding
- A subclass, class Savings(BankAccount):, reuses its parent's methods; defining a method with the same name overrides the parent's version.
- Polymorphism
- The same method call working on objects of different classes, each running its own version.
- Composition
- Building an object out of other objects held in its attributes (has-a), such as a Bank holding a list of accounts.
الملاحظات والمصادر
- a.deposit(50) is exactly equivalent to BankAccount.deposit(a, 50). That is why every method's first parameter, named self by convention, receives the object the method was called on.
- Python has no truly private attributes. A leading underscore, as in _balance, is a convention that marks a non-public part: code outside can still reach a._balance, but should not.
- When an object and its class lack an attribute, Python searches the parent classes in a fixed order, the method resolution order, shown by Savings.__mro__.
- "Favor object composition over class inheritance" is a principle from Gamma, Helm, Johnson and Vlissides, Design Patterns (1994). Think Python names the two relationships IS-A (inheritance) and HAS-A (composition).
- The cards indent by two spaces so each line fits. Python only needs the indentation to be consistent; the style guide PEP 8 recommends four spaces.
- The fee of 1 on Savings withdrawals is invented for the example.
- Python tutorial: 9. Classes
- Python tutorial: Inheritance
- Python tutorial: Private variables
- Python reference: object.__init__
- Python library: super()
- Python glossary: method resolution order
- PEP 8: Indentation
- Think Python 2e, ch. 17: Classes and methods (polymorphism)
- Think Python 2e, ch. 18: Inheritance (IS-A and HAS-A)
اختبار يتاح بعد الخطوة الأخيرة.