Skip to content

nos.common.metaclass

nos.common.metaclass

Common metaclasses for re-use.

SingletonMetaclass

Singleton metaclass for instantation of a single instance of a class.

For example
class Foo(metaclass=SingletonMetaclass):
    ...
Source code in nos/common/metaclass.py
class SingletonMetaclass(type):
    """Singleton metaclass for instantation of a single instance of a class.

    For example:
        ```python
        class Foo(metaclass=SingletonMetaclass):
            ...
        ```
    """

    _instance = None

    def __call__(cls, *args, **kwargs):
        """Call the class constructor."""
        if cls._instance is None:
            cls._instance = super().__call__(*args, **kwargs)
        return cls._instance

__call__

__call__(*args, **kwargs)

Call the class constructor.

Source code in nos/common/metaclass.py
def __call__(cls, *args, **kwargs):
    """Call the class constructor."""
    if cls._instance is None:
        cls._instance = super().__call__(*args, **kwargs)
    return cls._instance