python --- python decorator

  Mentioned decorator, I think is an interview two years ago, the interviewer asked me, what decoration is? Can you write a decorator do? Then gone then ~

Decorator

Decorator: Decorative object device. Possible without modifying the existing code, or add new features to additional restrictions or the object to be decorated decoration objects help output device. Possible without modifying the existing code, add new features or additional restrictions to the object to be decorated or help output

Usage: enter directly on the object to be decorated   @ decorator name  

Before writing a decorator, let's write a function named test, and then let anjing = test, so go call anjing found on the implementation of the function test

DEF test ():
     Print ( ' which is the name of a function test ' ) 

anjing = test 
anjing ()


 -------- ------ result 
which is a function named test

Here we write a function with parameters, then the above test function directly pass in

DEF test ():
     Print ( ' which is the name for the test functions ' ) 

DEF Anjing (A):   # A = test 
    Print (A)     # test memory address 
    A ()      # perform test 
    Print ( ' which is the name for the anjing function ' ) 

anjing (test)


 ---------- results ------ 
<AT 0x000000000040C1E0 function test> 
this is the name of a function test 
which is a function name anjing

Here you will find that we do not change the function test, but the test is decorated with some of the content (anjing function)

To demand the leadership to say, write automated, each use case plus print log and log upload the word success.

Some people say that this is not simply each with both plus print ( 'log upload and print logs') can not it do? But with a few cases, we can, but if you use very many cases, we have each written by this method? It is not too much trouble, since seen the decorators, we might as well write a decorator.

# Define a decorator 
DEF Fun (foo):
     DEF the Add ():
         Print ( ' journal printing ' ) 
        foo () 
        Print ( ' log upload successful ' )
     return the Add 

@fun 
DEF test01 ():
      Print ( ' This is the automation use cases 01 ' ) 
     
@fun      
DEF Test02 ():
      Print ( ' which is automation Example 02 ' ) 

Test01 () 
Test02 ()


 ---------- ------- result 
log printing 
this embodiment is automation 01 
log upload successfully  
log Print
this is automation use cases 02
Log uploaded successfully

May have a little ignorant small partners, we look at how this process go through the debug mode, and then analyzed

analysis:

1, the program ran, encountered the first function fun time will function first stored in the memory space, and then continue down

2, continue to go down, met @fun decorator, then continue to take the contents inside decorator.

3, the implementation of operating rules decorators, the need to decorate the function name as a parameter passed to the function fun, decorative function performs its own internal code, will assign its return value to the function to be decorated.

Decorator with parameters

Will find the above use case test01 no arguments, arguments that if so, how should this decorator go? We write with a parameter of test01

# Define a decorator 
DEF Fun (foo):
     DEF the Add (username):
         Print ( ' log Print ' ) 
        Result = foo (username)
         Print ( ' log successful upload ' )
         return Result
     return the Add 

@fun 
DEF Test01 (name):
      Print ( ' % S running automation embodiment 01 ' % name) 

Test01 ( ' anjing ' ) 


-------- ------ results

  Log Print
  anjing run automation use cases 01
  log upload success

This time certainly someone will ask, if you encounter some use cases pass a parameter, and some use cases passed two parameters, then how to get this time? It is actually very simple, ah, we are not in front of introduced non-fixed mass participation - * args, ** kwargs

# Define a decorator 
DEF Fun (foo):
     DEF the Add (* args, ** kwargs):
         Print ( ' journal printing ' ) 
        the Result = foo (* args, ** kwargs)
         Print ( ' log upload successful ' )
         return the Result
     return the Add 
@fun 
DEF test01 (name, Age):
      Print ( ' % S running automation use cases 01, and his age printed S% ' % (name, Age)) 

@fun 
DEF Test02 (name):
      Print ( ' % s to run the automation use cases 02' % Name,) 

Test01 ( ' anjing ' , 18 is ) 
Test02 ( ' ceshi ' )

In fact, everyone can write a decorator, but if you want to utilize in a scene decorator, this would require everyone to learn slowly, well, I was quiet, thank you for your attention

Guess you like

Origin www.cnblogs.com/qican/p/12573148.html