Python's @ symbol

Source: Internet
Author: User
Tags instance method

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.