Table of Contents

OOP

special methods

https://docs.python.org/3/reference/datamodel.html#special-method-names

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

delattr(object, name) This is a relative of setattr(). The arguments are an object and a string. The string must be the name of one of the object’s attributes. The function deletes the named attribute, provided the object allows it. For example, delattr(x, 'foobar') is equivalent to del x.foobar. name need not be a Python identifier (see setattr()).

Object-like attribute access for nested dictionary