Python 3 Deep Dive Part 4 - Oop
rect = Rectangle(3, 4) # OK
class Singleton: _instance = None def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = super().__new__(cls) return cls._instance python 3 deep dive part 4 oop
def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) PluginBase.registry.append(cls) print(f"Registered: cls.__name__") rect = Rectangle(3, 4) # OK class Singleton:
class PluginBase: plugins = [] def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) PluginBase.plugins.append(cls) rect = Rectangle(3
Use the @staticmethod decorator, acting like regular functions nested within a class, taking no implicit arguments. 4. Properties and Encapsulation