[Add] metaclass of singleton
This commit is contained in:
parent
6cfdeac8cb
commit
b05eb7142e
@ -445,3 +445,17 @@ def virtual(func: "callable"):
|
|||||||
that can be (re)implemented by subclasses.
|
that can be (re)implemented by subclasses.
|
||||||
"""
|
"""
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
||||||
|
class Singleton(type):
|
||||||
|
"""
|
||||||
|
Metaclass for creating singleton object.
|
||||||
|
"""
|
||||||
|
_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]
|
||||||
|
Loading…
Reference in New Issue
Block a user