Analysis of Common Usage Methods of python Decorator

  • 2021-07-01 07:55:15
  • OfStack

This paper describes the common use methods of python decorator with examples. Share it for your reference, as follows:

python decorator, can be used to achieve similar spring AOP similar functions. 1 can be used to record a method before the execution of what to do, after the execution of what to do, or to record logs, run time, etc. What's more, to use this to do permission interception, it is not impossible. Describe the decoration mode of python from two aspects:

1. Decoration of common methods

2. Decoration of methods in the class class, without the need to give parameters

3. For the decoration of methods in class class, it is necessary to give parameters

1. Decoration of common methods. For example, you want to calculate the execution time of 1-1 method.


#coding:utf-8
import time
def timeit(func):
  def wrapper(*args, **kv):
    start = time.clock()
    print ' Start execution '
    func(*args, **kv)
    end =time.clock()
    print ' Spend time :', end - start
  return wrapper
@timeit
def foo():
  print 'in foo()'
if __name__=='__main__':
  foo()

Run results:

Start execution
in foo()
Time spent: 6.55415628267 e-05

As you can see, the time difference is calculated. Instead of writing in a function like ordinary method 1.

2. Decoration of methods in the class class, without the need to give parameters


#coding:utf-8
import time
def timeit(func):
  def wrapper(*args, **kv):
    start = time.clock()
    print ' Start execution '
    func(*args, **kv)
    end =time.clock()
    print ' Spend time :', end - start
  return wrapper
class MySpendTime(object):
  def __init__(self):
    pass
  @timeit
  def foo(self):
    print 'in foo()'
spendtime=MySpendTime()
spendtime.foo()

Run results:

Start execution
in foo()
Time spent: 4.42208134735e-05

3. For the decoration of methods in class class, it is necessary to give parameters


#coding:utf-8
'''
Created on 2012-11-1
@author: yihaomen.com
'''
def UpdateUI(msg, step):
  print u" Content :", msg
  print u" Steps : To the first %s Step " % step
  def dec(func):
    def wapper(self, *args, **kwargs):
      func(self,*args, **kwargs)
    return wapper
  return dec
class Command(object):
  def Excute(self):
    self.Work1st()
    self.Work2nd()
    self.Work3rd()
  @UpdateUI(" Beginning 1 Step ","1")
  def Work1st(self):
    print "Work1st"
  @UpdateUI(" Beginning 2 Step ", 2)
  def Work2nd(self):
    print "Work2nd"
  @UpdateUI(" Beginning 3 Step ", 3)
  def Work3rd(self):
    print "Work3rd"
if __name__=="__main__":
  command = Command()
  command.Excute()

Run results:

Content: Start Step 1
Step: Step 1
Content: Start Step 2
Step: Step 2
Content: Start Step 3
Step: Step 3
Work1st
Work2nd
Work3rd

For more information about Python, please see the topics of this site: "Python Data Structure and Algorithm Tutorial", "Python Socket Programming Skills Summary", "Python Function Use Skills Summary", "Python String Operation Skills Summary" and "Python Introduction and Advanced Classic Tutorial"

I hope this article is helpful to everyone's Python programming.


Related articles: