What is the flow and principle of python's decorator if it is a class?


class Singleton:

    """
    
    """

    def __init__(self, cls):
        """  """
        self._cls = cls

    def Instance(self):
        """
        
        """
        try:
            return self._instance
        except AttributeError:
            self._instance = self._cls()
            return self._instance

    def __call__(self):
        raise TypeError("Singletons must be accessed through `Instance()`.")

    def __instancecheck__(self, inst):
        return isinstance(inst, self._decorated)


-sharp 
@Singleton
class A:
    """"""
    def __init__(self):
        pass

    def display(self):
        return id(self)

if __name__ == "__main__":
    s1 = A.Instance()
    s2 = A.Instance()
    print(s1, s1.display())
    print(s2, s2.display())
    print(s1 is s2)

this is the singleton pattern implementation seen on the Internet, his decorator is a class, but what I see about the decorator on the Internet are all functions. According to my understanding, if the decorator is a function, the following function will be passed into the @ function, and the @ function will return a function, which will perform the passed function, and some other operations. But what if @ is a decorator? according to this code, A should be passed into the _ _ init__ method of Singleton, but the call is A.Instance (),. Is this an inheritance relationship? what does _ _ instancecheck__ do? what does self._decorated mean? I really can"t understand

Mar.11,2021

here is a personal understanding of the code, hoping to provide you with some clues:

When

@ Singleton decorates class A, giving Singleton (self._cls) with class An as a parameter, the Singleton class is instantiated and named A.
then A.instance () calls the instance () method to instantiate the self._cls in An of the Singleton class, so that the instance of classA becomes a member property of instance An of the Singleton class. This is a bit like inner class, or nested class,inner class can use the properties and methods of external class, but should be different from inheritance.

the following paragraph should look like this: self._decorated is actually self._cls,. I found similar code on the following web page and changed the self._decorated, to run the same result. I don't know why.
http://outofmemory.cn/code-sn.

def _ _ instancecheck__ (self, inst):

    return isinstance(inst, self._decorated)

_ _ instancecheck__ (self, inst): is the magic method of python, which is triggered when print (isinstance (s2jue A) is executed to determine whether its actual instance is an instance of a class.

change def instance () to this:
def Instance (self):

        self._instance = self._cls()
        return self._instance
        

running S1 is S2 returns False.
We know that there is no change before, because try except will let instance () return self._instance, implement one if it doesn't have one. As a result, S1 and S2 id are the same, my understanding should be the same instance, S1 is S2 returns True. After the
is changed, S1 and S2 will have a different id, and S1 is S2 will return False.

call Let A () sell, so use instance () to implement class by decorated

Menu