meta data for this page
This is an old revision of the document!
OOP
special methods
call super constructor
class A(object): def __init__(self): print("world") class B(A): def __init__(self): print("hello") super().__init__()
singletons
class MySingleton: instance = None def __new__(cls, *args, **kwargs): if not isinstance(cls.instance, cls): cls.instance = object.__new__(cls) return cls.instance
class Singleton(type): _instances = {} def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super (Singleton,cls).__call__(*args, **kwargs) return cls._instances[cls] class SerialNumber(metaclass=Singleton):
attributes / properties
- attribute - direct access to data member of object
- property - properties are methods accessed like attributes. It gives full control on its getter, setter and deleter access.