|
|
|
Shouldn't the instance be kept on the 'self' variable and not the metaclass instance? Otherwise all users of the metaclass will reuse the same instance. In the metaclass 'self' should be renamed 'cls' or 'klass' to make the intent clearer.
class SingletonMeta(object):
__def __call__(cls):
____if getattr(cls, "instance", None) is None:
______cls.instance = type.__call__(cls)
____return cls.instance
And yes, singletons are usually an anti-pattern but like globals sometimes they're handy!
Jack Diederich |
Homepage |
07/04/23 - 7:14 pm | #
|
|
You're right of course. I've changed the example, thanks.
Fuzzyman |
Homepage |
07/04/23 - 10:08 pm | #
|
|
|
Commenting by HaloScan
|