Python Basics "6th article": Python Decorator

Source: Internet
Author: User

Decorative Device

Adorner mode (Decorator pattern) allows you to add new functionality to an existing object without changing its structure. This type of design pattern belongs to the structural pattern, which is a wrapper as an existing class.

This pattern creates an adornment class that wraps the original class and provides additional functionality, while preserving the integrity of the class method signature.

For example, explain:

Def func1 ():    some options

We now have a function that has been running for a period of time func1, one day we need some new features, because can not directly modify the line function func1, how to do? Then we can use the adorner.

Define a new adorner function decorater

def Auth (func):     def inner ():         Print " So some things here "         func ()    return  inner@authdef  func1 ():     Print  func1 result"func1 ()

Execution results

So some things herethis is func1 resule

Explanation: The adorner function receives the decorated function name, executes the internal inner function, and returns the inner corresponding memory address. The inner function blocks can be used to write new functions. When we make a new function call, we can execute the new function without modifying the original function, and the user does not need to make any code changes when making the call.

Defect: Unable to pass parameter.

Functions for decorating with parameters

When we define a function, we often need to pass the argument, how does the adorner decorate the function with parameters? Add the adorner to the function! is an adorner with parameters.

defAuth (func):defInner (argv): This is used to receive parameters to pass parameters to the decorated func function behindPrint "So some things here"func (argv)returnInner@authdeffunc1 ():Print "func1 Result"    PrintArgvfunc1 ('argv1')

Execution Result:

So some things herefunc1 resultargv1

in the same vein, if we need to pass multiple parameters or dynamic parameters, we can modify the adorner memory function inner to receive multiple parameters or dynamic parameters. And the adorner automatically matches the dynamic parameters to the corresponding function. that is, if some functions do not require parameters, we can use only one adorner.

def Decorater (func):     def Inner (*args,**Kwargs):        //Dosome options here        func (*args,**kwargs)

Now we have a problem, our function has no return value, can we decorate the function with return?

Adorner trim with return function parameters

In the code:

 def   Auth (func):  def   Inner ():  print   " so some things here  "   func ()  return   Inner@auth  def   Func1 ():  print   " this is func1 result  "  return   Resultfunc1 ()  

Here the function func1 with the return function, but in the actual operation we use the adorner but not get the return value, why? Since we used the adorner and our original function became part of the adorner's inner function, the inner function received the return value of func1, but inner did not return, so we get the return of none.

So as long as the return value of the FUNC1 function obtained by the inner function is resumed, it can be obtained.

Multi-layer Adorner execution order

When the adorner has multiple layers, that is, when more than one adorner decorates the same function, the order is executed from top to bottom.

Adorner function parameters

@wapper (FUNC1,FUNC2)

Execution order: 1. Executive Wapper (FUNC1,FUNC2) 2. The result of the previous step is equal to a target result such as RET 3. Execute @ret Adorner

That is, the entire process dynamically generates an adorner.

Python Basics "6th article": Python Decorator

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.