An interesting example of Python decorators and an exploration of python decorations

Source: Internet
Author: User

An interesting example of Python decorators and an exploration of python decorations

Miss Liao's tutorial is too advanced to understand. Click to open the link.

def deco_functionNeedDoc(func):    if func.__doc__ == None :        print func, "has no __doc__, it's a bad habit."    else:        print func, ':', func.__doc__, '.'    return func@deco_functionNeedDocdef f():    print 'f() Do something'@deco_functionNeedDocdef g():    'I have a __doc__'    print 'g() Do something'f()g()print fprint g

The output of this Code is as follows:

<Function f at 0x0238F930> has noDoc, It's a bad habit.

<Function g at 0x0238F8B0>: I haveDoc. F () Do somethingg () Do something

<Function f at 0x0238F930>

<Function g at 0x0238F8B0>

At that time, I was dizzy. After thinking for a long time, I used to call the decorator when I called the @ decorator function. The decorator function return func, and func is the input parameter f. Modify the code at this time.

 

def deco_functionNeedDoc(func):    if func.__doc__ == None :        print func, "has no __doc__, it's a bad habit."    else:        print func, ':', func.__doc__, '.'    def func_1(*args, **kw):        print func.__name__,' : this is func_1'    return func_1@deco_functionNeedDocdef f():    print 'f() Do something'@deco_functionNeedDocdef g():    'I have a __doc__'    print 'g() Do something'f()g()print fprint g

Print the result at this time:

<Function f at 0x023CF930> has no _ doc __, it's a bad habit.
<Function g at 0x023CF8B0>: I have a _ doc __.
F: this is func_1
G: this is func_1
Func_1
Func_1


At this point, the problem should be clear ~~ Just, what is the ornament ?? Under what circumstances ?? Waiting for exploration


Finally, I posted another super reunion, which is very interesting. I don't want to explain it, but I am sleepy. After understanding this, I can fully evaluate the basic principle of the decoration device.

def deco_functionNeedDoc(func1):    if func1.__name__ == "yyy" :        print func1, "the func1 == yyy"    else:        print func1, ':', func1.__doc__, '.'    def xxx() : # y + f + h + x        func1() # y + f + h + f + h        print "plus xxx"        return func1() # y + f + h + f + h    return xxxdef deco_functionNeedDocxxx(func2):    if func2.__name__ == "hhh" :        print func2, "the func2 == hhh"    else:        print func2, ':', func2.__doc__, '.'    def yyy(): # y + f + h + f + h        print "plus yyy"        func2()  # f + h        return func2() # f + h    return yyydef deco_functionNeedDochhh(func3):    if func3.__name__ == "f" :        print func3, "the func3 == f"    else:        print func3, ':', func3.__doc__, '.'    def hhh() : # = f + h        func3()  # = f        print "plus hhh"        return "Hello Wrold"    return hhh@deco_functionNeedDoc@deco_functionNeedDocxxx@deco_functionNeedDochhhdef f():    print 'print original fff'f()print "------------"print fprint "------------"print f()

The result is as follows:

<Function f at 0x0000a2b938> the func3 = f
<Function hhh at 0x0000a2b9b0> the func2 = hhh
<Function yyy at 0x0000a2ba28> the func1 = yyy
Plus yyy
Print original fff
Plus hhh
Print original fff
Plus hhh
Plus xxx
Plus yyy
Print original fff
Plus hhh
Print original fff
Plus hhh
------------
<Function xxx at 0x0000a2baa0>
------------
Plus yyy
Print original fff
Plus hhh
Print original fff
Plus hhh
Plus xxx
Plus yyy
Print original fff
Plus hhh
Print original fff
Plus hhh
Hello Wrold

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.