Home > front end >  How can show the singleton object's attribution?
How can show the singleton object's attribution?

Time:10-13

We can show instance's attribution with dir function.

>>> class mytest():
...     test = 1
...     def __init__(self):
...         pass
>>> x=mytest()
>>> x.test
1
>>> dir(x)[-1]
'test'

Now create a class with metaclass method:

class Singleton(type):
    _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]

class Cls(metaclass=Singleton):
    pass

Show Cls's _instances attrubution:

Cls._instances
{<class '__main__.Cls'>: <__main__.Cls object at 0x7fb21270dc40>}

Why no string _instances in dir(Cls)?

>>> dir(Cls)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__',  
 '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', 
'__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', 
'__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__setattr__', '__sizeof__', '__str__', 
'__subclasshook__', '__weakref__']
>>> Cls.__dict__
mappingproxy({'__module__': '__main__', '__dict__': <attribute '__dict__' of 'Cls' objects>, 
'__weakref__': <attribute '__weakref__' of 'Cls' objects>, '__doc__': None})    

CodePudding user response:

Because it's stored on the metaclass.

>>> '_instances' in dir(Singleton)
True
>>> Singleton._instances
{<class '__main__.Cls'>: <__main__.Cls object at 0x7fb21270dc40>}

To be clear, this has nothing to do with the singleton aspect.

  • Related