Classes and Data Model

More about Python Data Model you may read in the official documentation

Using namespace classes

This classes allow you directly assign params to class

from types import SimpleNamespace
class S(SimpleNamespace):
    pass

s = S(some='some', value='42')
print(s.some)
# out: 'some'
print(s.value)
# out: '42'

can be replaced such way:

class adict(dict):
    def __init__(self, *av, **kav):
        dict.__init__(self, *av, **kav)
        self.__dict__ = self

Slots Usage

>>> class Point:
...     __slots__ = ('x', 'y')
...
>>> p = Point()
>>> p.x = 1
>>> p.y = 2
>>> p.z = 33
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Point' object has no attribute 'z'

TODO: add singleton, metaclasses examples