design_pytterns.interfaces package

SubclassIdentificable class

class design_pytterns.interfaces.SubclassIdentifiable

Bases: object

Inject CLASS_ID class attribute to all the subclasses of the base.

The subclasses of the base that inherits from this class require the class_id keyword argument in the class definition with a Hashable not None value.

    Changed in version 0.4.0:
  • raise UnhashableClassIdError instead of TypeError.

  • raise InvalidClassIdError instead of ValueError.

Raises
  • UnhashableClassIdError – If class_id is not Hashable.

  • InvalidClassIdError – If class_id is None for any of the subclasses.

Examples

>>> from design_pytterns.interfaces import SubclassIdentifiable
>>>
>>> class MyBase(SubclassIdentifiable):
...    pass
...
>>> class MyIdentifiableSubclass(MyBase, class_id=1):
...    pass
...
>>> MyIdentifiableSubclass.CLASS_ID
1