how to implement singleton
in python
?
there is really lot of way to do. even there is a library.
(even if this library's last update is 3 years ago...)
this moment i do not fully understand those codes.
but i need to implement code. so i choose some alternative.
...
_instance = None
@classmethod
def _getInstance(cls):
return cls._instance
@classmethod
def instance(cls, *args, **kargs):
cls._instance = cls(*args, **kargs)
cls.instance = cls._getInstance
return cls._instance
...
this code has critical cons that is can not use constructor
. so i just call __init__
like method once.
@classmethod
def init_like(cls):
# initialize instance
pass
i know this is bad. and quite ugly ☹️
so i refactor this part later.