class documentation
class Singleton(type): (source)
Singleton metaclass.
This class implements the Singleton design pattern as a metaclass.
Attention
Classes using this metaclass will never be able to have more than one instance.
Also, instances will not be garbage collected until the program ends.
Example
- class MyClass(metaclass=Singleton):
- pass
| Method | __call__ |
Override the __call__ method to control the instantiation of classes using this metaclass. |
| Class Variable | _instances |
Dictionary to hold the single instances of the classes using this metaclass. |