Interesting examples of Python adorners

Source: Internet
Author: User

Teacher Liao's tutorial is too advanced, do not 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 had a __doc_ _ '    print ' g () do something ' F () g () Print Fprint g

This code prints the following results:

<function F at 0x0238f930> have no Doc , it ' s a bad habit.

<function g at 0x0238f8b0>: I has a doc . f () do somethingg () do something

<function F at 0x0238f930>

<function G at 0x0238f8b0>

At that time I was dizzy, thought for a long time, originally in the @ Adorner function will invoke the adorner, the adorner function return func, and Func is passed in the parameter F. Change 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 had a __doc__ '    print ' g () do something ' F () g () Print Fprint g

Print results at this time:

<function f at 0x023cf930> have no __doc__, it ' s a bad habit.
<function g at 0x023cf8b0>: I has a __doc__.
F:this is func_1
G:this is func_1
Func_1
Func_1


The problem is, it should be very clear ~ ~ Just, what do the decorators do?? Under what circumstances is it applied?? Pending exploration


Finally paste a super reunion, too interesting, this does not explain, sleepy, understand this, reckoned the basic principle of the decorator is thorough.

def deco_functionneeddoc (func1): if func1.__name__ = = "YYY": Print Func1, "the func1 = = yyy" Else:pr    int 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 yyy def deco_functionneeddochhh (func3): if func3.__name__ = = "F": Print Func3, "the func3 = = f" Else:pri    NT Func3, ': ', func3.__doc__, '. ' Def HHH (): # = f + H func3 () # = f print "plus hhh" return "Hello wrold" return Hhh@deco_functio Nneeddoc@deco_functionneeddocxxx@deco_functionneeddochhhdef f (): print ' Print original FFF ' F () print "------------" Print Fprint "------------"Print f () 

The results are as follows:

<function f at 0x109a2b938> the func3 = = f
<function HHH at 0x109a2b9b0> the FUNC2 = = HHH
<function yyy at 0x109a2ba28> 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 0x109a2baa0>
------------
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

Interesting examples of Python adorners

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.