Descriptors define __get__, __set__, __delete__ methods to customize attribute access. Used in properties, methods, class attributes. Enable managed attributes, validation, computed values. Example: property implementation using descriptors. Consider performance implications.
ABCs define interface through @abstractmethod, can include implementation. Created using abc.ABC. Enforce interface implementation, provide default methods. Unlike Java interfaces, can have implementation. Consider multiple inheritance implications.
Implement __iter__ and __next__ methods for iteration. Consider StopIteration handling. Implement proper cleanup. Handle resource management. Consider memory efficiency. Example: custom sequence types.
Implement rich comparison methods (__eq__, __lt__, etc.). Use @functools.total_ordering for complete ordering. Consider type checking, reflexivity, transitivity. Handle edge cases. Implement proper comparison logic.
Bound methods are instance-specific, include self parameter automatically. Unbound methods are class-level, require explicit instance. Consider method types, descriptor protocol. Handle method binding properly.
Implement __getstate__ and __setstate__ for custom serialization. Handle complex objects, circular references. Consider security implications. Implement proper state management. Handle version compatibility.
Type hints provide static typing information. Used by type checkers (mypy), IDE support. Not enforced at runtime by default. Enhance code readability, maintainability. Consider performance impact, compatibility.
Generics provide type hints for collections/classes. Use typing.Generic, TypeVar. Support parametric polymorphism. Consider type checking implications. Implement proper type constraints. Handle type variables.
Protocols define interface behavior without inheritance. Use typing.Protocol for static typing. Support structural subtyping. Consider duck typing implications. Implement protocol compatibility.
Use __slots__, proper data structures, generators. Consider memory profiling, garbage collection. Handle large datasets efficiently. Implement proper cleanup. Monitor memory usage. Consider caching strategies.
Assignment expressions (:=) assign and return value in single expression. Used in while loops, if statements, list comprehensions. Enhances code readability, reduces redundancy. Consider proper usage contexts.
@dataclass decorator provides automatic __init__, __repr__, etc. Support inheritance, frozen instances, default values. Consider field options, ordering. Implement custom initialization. Handle field dependencies.
Use decorator pattern, functools.wraps for metadata preservation. Handle argument passing, return values. Consider function attributes. Implement proper wrapping. Handle special methods.
Metaclasses are classes for classes, allowing customization of class creation. Used for API enforcement, attribute validation, class registration, abstract base classes. Created using type or custom metaclass. Example: class MyMetaclass(type): def __new__(cls, name, bases, attrs). Consider complexity trade-offs.
__new__ handles object creation (called before __init__), returns instance. __init__ initializes instance (called after __new__). __new__ used for immutable types, singletons, metaclasses. __init__ can't return value. Consider inheritance implications.
Uses reference counting for basic GC, generational GC for cycles. Reference count tracks object references, frees when zero. Cycle detector identifies and collects unreachable reference cycles. Consider memory management, performance implications.
Implement __enter__ and __exit__ methods, or use contextlib.contextmanager decorator. Handle resource acquisition/release, exception handling. Consider cleanup guarantees. Example: file handling, database connections.
MRO determines method lookup order in inheritance. Uses C3 linearization algorithm. Accessible via __mro__ attribute. Handles multiple inheritance, method overriding. Consider inheritance complexity, diamond problem.
send() method for two-way communication, throw() for exception injection, close() for generator cleanup. Generator delegation using yield from. Consider coroutine behavior, exception handling. Implement proper cleanup.
Class decorators modify/enhance class definitions. Applied using @decorator syntax above class. Can modify class attributes, methods, add functionality. Consider inheritance implications. Handle class modification properly.