Python has always been used, did not go to the system to learn, in an interview saw the @ symbol, came back to see, the symbol used in the adorner, used to decorate a function, the modified function as a parameter passed to the adorner, the following examples:
1. @classmethod and @staticmethod
These two meanings are obvious, when defining the method @classmethod means that the method is a class method, the class method must have a parameter of CLS, the class itself, the first argument of the instance method is the [email protected] Adornment method is basically the same as a global function.
Both of these modified methods are available through instance and class invocation.
Class A (): @classmethod def CLASSM (CLS): print "class method, and Invoker:", cls.__name__ @ Staticmethod def staticm (): print "static Method" class B (A): PASSA.CLASSM () #class method, and Invoker:AB.classM () #class method, and Invoker:BA.staticM () #static methodb.staticm () #static methoda=a () A.CLASSM () #class method, and Invoker:Aa.staticM () #static methodb=b () B.CLASSM () #class method, and Invoker: BB.STATICM () #static method
2. As a normal modifier, the following definition is similar to Testone=func (Testone)
Class C (): def func (FN): def Test (*args): print "Hello" return test @func def testone ( b): print a**2+b**2 if __name__== "__main__": testone (3,4) #output: Hello
Class C (): def func (FN): def Test (*args): print "Hello" fn (*args) return test @func def Testone (A, b): print a**2+b**2 if __name__== "__main__": testone (3,4) #output: hello25
3. Unusual notation, used to modify a class, can be used in a single-case mode
Def Singleton (CLS): instance={} def getinstance (): If CLS not in instance: instance[cls]=cls () return INSTANCE[CLS] return getinstance@singletonclass Myclass: pass#output>>> my1= Myclass () >>> print my1<__main__. Myclass instance at 0x00000000028c2f48>>>> my2=myclass () >>> print my2<__main__. Myclass instance at 0x00000000028c2f48>
Python's @ symbol