Python-based decorator detailed

Source: Internet
Author: User

The adorner's syntax begins with @, followed by the name of the adorner function, and optional parameters.

Following the adorner declaration is the optional parameters of the decorated function and the decorated function, as follows:

@decorator (Dec_opt_args) def func (Func_args): ....

In fact, generally speaking, the adorner is actually a function, a function used to wrap the function, the adorner is called when the function declaration is completed, and the function declared after the call is replaced by a function that has been decorated by the adorner.

Such as:

def deco (func): ... return func@decodef foo (): print ' foo ' #-----------------------------------#等价如下: def deco (func ): ... return funcdef foo (): print ' foo ' foo = Deco (foo)

Here's an example:

def deco1 (func): print ' OK ' return func@deco1def foo (): print ' foo ' foo () #输出--------------#ok #foo#---------------- --


If you do not use adorners, you can do the following:

def deco1 (func): print ' OK ' return funcdef foo (): print ' foo ' print foo #<function foo at 0x00afe6f0& Gt;foo = Deco1 (foo) foo () #输出--------------#ok #foo#------------------


In contrast, it can be found that the use of adorners is so simple and flexible. Especially in enterprise-level development.

It is also possible to overlap multiple adorners:

def deco1 (func): print ' Deco1 ' return funcdef Deco2 (func): print ' Deco2 ' return func @deco1 @deco2def foo (): print ' foo ' foo () #输出如下:-----------#deco2 #deco1#foo#---------------------

is equivalent to:

@deco1 @deco2def foo (ARG):p the-----------and inferior effect----------foo = deco1 (Deco2 (foo ()))


Two, a parameter, no parameter of the adorner

The above examples are basically parameters, and no parameters are simpler.

1. No reference

@deco1 @deco2def foo (ARG):p the---------------------

Foo = Deco1 (Deco2 (foo ()))

2. Have a reference

@deco1 (deco_arg) @deco2def foo (ARG):p

---------------------

Foo = Deco1 (Deco_arg) (Deco2 (foo ()))

Returns an adorner with a function as a parameter

Third, use

1. Reference log

2. Increase timing logic to detect performance

3. Ability to add transactions to functions

Iv. examples

From time import ctime,sleepdef Deco (func): Def decoin (): print ' [%s]:%s called '% (CTime (), func.__name__) return func return decoin@decodef foo (): Passfoo () sleep (4) for I in Range (2): Sleep (1) foo () #输出如下:------ --#[fri Jul 10:45:04 2013]:foo called#[fri Jul 10:45:09 2013]:foo called#[fri Jul 10:45:10 2013]:foo called#----- -------------


Python-based decorator detailed

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.