Python functional Programming--decorators

Source: Internet
Author: User

1.1   Decorative Device

A function object can be assigned to a variable, so the function can also be called by a variable.

>>> def now ():

.. print (' 2016 ')

...

>>> Now ()

2016

>>> F = Now--The function object assigns a value to the variable

>>> f ()-- call

2016

of the Function object __name__ , can get the name of the function

>>> now.__name__

' Now '

>>> f.__name__

' Now '

Suppose we want to enhance the function of the now () function, for example, to automatically print the log before and after a function call, but do not want to modify the definition of the now () function, a way to dynamically add functionality during the run of the code, called an "adorner" ( Decorator).

Decorator is a higher-order function that returns a function . Define a decorator as follows

>>> def log (func):

... def wrapper (*args, **kw):-- the previous variable parameter and keyword Parameters section has been explained,= can pass any parameter

... print (' Call%s (): '% func.__name__)

... return func (*args, **kw)

... return Wrapper

...

>>> @log-- equivalent to executing now = log (now), redefining the now function.

... def now ():

.. print (' 2016 ')

...

>>> Now ()

Call Now ():

2016

#################################################################

>>> def log2 (func):

... def wrapper (*args, **kw):

... print (' Begin call ')-- execute before calling

... res = func (*args, **kw)

... print (' End call ')-- execution after invocation

... return Res

... return Wrapper

>>> @log2

... def PR ():

.. print (' 2016 ')

...

>>>

>>> PR ()

Begin Call

2016

End Call

##################################################################

>>> def log (text):-- custom text content

... def decorator (func):

... def wrapper (*args, **kw):

... print ('%s%s (): '% (text,func.__name__))

... return func (*args, **kw)

... return Wrapper

... return Decorator

...

>>>

>>>

>>>

>>> @log (' execute ')-- equivalent to execute now=log (' Execute ') (now)

... def now ():

.. print (' 2016 ')

...

>>> Now ()

Execute Now ():

2016

>>> now.__name__

' Wrapper '-- returns the function name, but wrapper

##################################################################

>>> Importfunctools

>>>

>>> def log (func):

... @functools. Wraps (func)

... def wrapper (*args, **kw):

... print (' Call%s (): '% func.__name__)

... return func (*args, **kw)

... return Wrapper

...

>>>

>>>

>>> @log

... def now ():

.. print (' 2 ')

...

>>> now

<function now at 0x2ab1090d9e18>

>>> Now ()

Call Now ():

2

>>> now.__name__

' Now '

##################################################################

>>> Importfunctools

>>> def log (text):

... def decorator (func):

... @functools. Wraps (func)

... def wrapper (*args, **kw):

... print ('%s%s (): '% (text,func.__name__))

... return func (*args, **kw)

... return Wrapper

... return Decorator

...

>>> @log (' Run ')

... def now ():

.. print (' 2016 ')

...

>>> Now ()

Run Now ():

2016

>>> now.__name__

' Now '

just remember to add @functools. Wraps (func) in front of the definition wrapper () .


This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1820989

Python functional Programming--decorators

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.