Use python parsing function modifier @

This article describes how to use python function modifier @ resolution, the text introduced by the sample code is very detailed, with some reference value of learning for all of us to learn or work, a friend in need can refer to the
python function of modifier @ It is to increase additional functionality to existing functions, used for insertion logs, performance test, the transaction processing and the like.

Rules create a function modifier:
(1) is a function modifier
(2) be modified to take modifier function parameters
(3) returns a new function modifier
(4) Maintenance modifier function signature is maintained

Example 1: was modified with no arguments

def log(func):
  def wrapper():
    print('log开始 ...')
    func()
    print('log结束 ...')
  return wrapper
@log
def test():
  print('test ..')
test()

operation result:

log开始 ...
test ..
log结束 ...

Example 2: Use property modification function module provides a method functools wraps

def log(func):
  def wrapper():
    print('log开始 ...')
    func()
    print('log结束 ...')
  return wrapper
@log
def test1():
  print('test1 ..')
 
def test2():
  print('test2 ..')
print(test1.__name__)
print(test2.__name__)

operation result:

wrapper
test2

Test1 function name becomes visible, and if some of the code used will be a problem, a function may be used to modify the properties functools module provides a method wraps

from functools import wraps
 
def log(func):
  @wraps(func)
  def wrapper():
    print('log开始 ...')
    func()
    print('log结束 ...')
  return wrapper
@log
def test1():
  print('test1 ..')
 
def test2():
  print('test2 ..')
 
print(test1.__name__)
print(test2.__name__)

operation result:

test1
test2

Example 3: Function parameters are modified

from functools import wraps
def log(func):
  @wraps(func)
  def wrapper(*args,**kwargs):
    print('log开始 ...',func.__name__)
    ret = func(*args,**kwargs)
    print('log结束 ...')
    return ret
  return wrapper
@log
def test1(s):
  print('test1 ..', s)
  return s
 
@log
def test2(s1, s2):
  print('test2 ..', s1, s2)
  return s1 + s2
test1('a')
test2('a','bc')

operation result:

log开始 ... test1
test1 .. a
log结束 ...
log开始 ... test2
test2 .. a bc
log结束 ...

Example 4: modifier parameters, requires more than the above example a multi-layer packaging

from functools import wraps
 
def log(arg):  
  def _log(func):
    @wraps(func)
    def wrapper(*args,**kwargs):
      print('log开始 ...',func.__name__, arg)      
      ret = func(*args,**kwargs)
      print('log结束 ...')
      return ret
    return wrapper
  return _log
  
@log('module1')
def test1(s):
  print('test1 ..', s)
  return s
 
@log('module1')
def test2(s1, s2):
  print('test2 ..', s1, s2)
  return s1 + s2
test1('a')
test2('a','bc')

operation result:

log开始 ... test1 module1
test1 .. a
log结束 ...
log开始 ... test2 module1
test2 .. a bc
log结束 ...

We recommend the python learning sites to see how seniors are learning! From basic python script, reptiles, django, data mining, programming techniques, as well as to combat zero-based sorting data items, given to every love learning python small partner! Python veteran day have to explain the timing of technology, to share some of the ways to learn and need to pay attention to small details, click on Join us python learner gathering

Published 39 original articles · won praise 38 · views 50000 +

Guess you like

Origin blog.csdn.net/haoxun05/article/details/104450200