I saw many ways of making a singleton in Python and I tried to use MetaCLAS implementation with Python 3.2 (windows). , But it does not seem to back up the same example of my singleton class. class singleton (type): _instances = {} def __call __ (cls, * args, ** kwargs): if not in cls_instances: cls._instances [cls] = super (singleton , CLS) .__ call __ (* args, ** kwargs) return cls._instances [CLS] class MyClass (object): __metaclass__ = singleton = a = MyClass () B = Maclus () print (A b is) # incorrect < / Code>
I'm working now which use decorator implementation, but I'm wondering what is wrong with this implementation?
See MetaClass syntax in Python 3.
class MyClass (metaclass = singleton): pass and it works:
& gt; & Gt; & Gt; MyClass () is true
No comments:
Post a Comment