English
Summary
Conclusions
A class defines a contract for state and behavior. The constructor establishes a valid initial state, and methods and properties maintain invariants. Encapsulation conventions help developers collaborate; they do not protect against deliberate access. Object relationships and UML help explain responsibilities before all methods are written.
Self-check questions
- How does a class differ from an instance?
- What responsibilities do
__new__and__init__have? - How is
selfpassed in a bound method? - Why does an attribute annotation not create a value?
- When does an instance attribute shadow a class attribute?
- Why does a class-level list become shared among objects?
- What priority does a data descriptor have?
- What does
__slots__provide, and what does it not provide? - How do
_nameand__namediffer? - How do you avoid recursion in a setter?
- When does a cached property become stale?
- How do you choose between a method, classmethod, and staticmethod?
- How do
__str__and__repr__differ? - How do you explain aggregation and composition with UML?
- Why does
delnot guarantee immediate finalization?