OOP Highlights Summary
Every highlight bullet from the 12 pages in this section, gathered on one page and grouped by the page it came from.
- A class is itself an object - an instance of type - which is why classes can have their own attributes
- Attribute lookup checks the instance dict, then walks the class's MRO, with descriptors able to intercept it
- Dunder methods are the mechanism Python's syntax (+, len(), for, with) uses to talk to arbitrary objects
- dataclasses and Protocols don't replace this model - they generate or structurally satisfy it
- Classes are callable factories for instances
- self is explicit first parameter of instance methods
- Prefer composition over deep inheritance
- dataclasses reduce boilerplate for data records
- Dunder methods hook into Python syntax
- eq and hash must stay consistent
- repr targets developers; str targets users
- collections.abc documents container protocols
- dataclass generates init, repr, comparisons
- field(default_factory=list) avoids mutable default trap
- frozen=True makes hashable value objects
- post_init validates after field assignment
- @property wraps getter/setter/deleter
- Descriptors power property, classmethod, staticmethod
- set_name wires descriptor to attribute name
- Validate in setter, not at every read site
- MRO is C3 linearization of base classes
- super() follows MRO, not just parent
- Mixins should be small and focused
- Prefer composition when inheritance depth grows
- ABC prevents instantiation until abstract methods exist
- @abstractmethod documents required overrides
- collections.abc defines container protocols
- register() enables virtual subclasses
- Protocol matches by method shape, not inheritance
- @runtime_checkable enables isinstance at runtime
- Structural typing reduces ABC ceremony
- Use for third-party types you cannot subclass
- Instance methods receive self
- classmethod receives cls - common for factories
- staticmethod has no implicit first arg
- Use classmethod for alternative constructors
- Enum members are singletons
- StrEnum compares cleanly to strings
- match/case exhaustiveness with enums
- Flag supports bitwise combinations
- init_subclass replaces most metaclass needs
- Metaclasses customize class creation
- Reach for registration hooks before metaclasses
- Over-metaprogramming hurts debuggability
- Prefer composition and protocols over deep trees
- dataclasses for data; functions for transforms
- Explicit repr and small public APIs
- Avoid metaclasses unless simpler hooks fail
636f64656775696465732e696f7c6367696f3734337c323032363037
Revisado por Chris St. John·Última atualização: 31 de jul. de 2026