Example analysis of the execution process of Python decorator

不言
Release: 2018-06-04 11:57:11
Original
1389 people have browsed it

This article mainly introduces the execution process of Python decorator, and analyzes the principle, execution process and related operation precautions of Python decorator in the form of examples. Friends in need can refer to the following

Example analysis of this article The execution process of Python decorator. I share it with you for your reference. The details are as follows:

I saw a sentence today: Decorators are actually the use of closures. If you think about it carefully, this is actually what it is. Today I looked at closures again. Basically, we have figured out the execution process of closure. In fact, it can be easily discovered after adding a few sentences. It is best to summarize the ideas for readers to help their understanding. Let’s talk about it through code.

The first type, the decorator itself does not pass parameters, the process is relatively simple

#!/usr/bin/python
#coding: utf-8
# 装饰器其实就是对闭包的使用
def dec(fun):
  print("call dec")
  def in_dec():
    print("call in_dec")
    fun()
  # 必须加上返回语句,不然的话会默认返回None
  return in_dec
@dec
def fun():
  print("call fun")
# 注意上面的返回语句加上还有不加上的时候这一句执行的区别
print(type(fun))
fun()
'''
通过观察输出结果可以知道函数执行的过程
call dec
<type &#39;function&#39;>
call in_dec
call fun
观察这几组数据以后,其实很容易发现,先执行装饰器,执行过装饰器以后,代码继续执行最后的print和fun()语句,
但是此时的fun函数其实是指向in_dec的,并不是@下面的fun函数,所以接下来执行的是in_dec,在in_dec中有一个fun()语句,
遇到这个以后才是执行@后面的fun()函数的。
&#39;&#39;&#39;
Copy after login

The second type, the decorator itself passes parameters, Personally, I think it’s relatively complicated. It’s best to summarize this process yourself. If you have any questions, let’s discuss them together

#!/usr/bin/python
#coding: utf-8
import time, functools
def performance(unit):
  print("call performance")
  def log_decrator(f):
    print("call log_decrator")
    @functools.wraps(f)
    def wrapper(*arg, **kw):
      print("call wrapper")
      t1 = time.time()
      t = f(*arg, **kw)
      t2 = time.time()
      tt = (t2 - t1) * 1000 if unit == "ms" else (t2 - t1)
      print &#39;call %s() in %f %s&#39; % (f.__name__, tt, unit)
      return t
    return wrapper
  return log_decrator
@performance("ms")
def factorial(n):
  print("call factorial")
  return reduce(lambda x, y: x * y, range(1, 1 + n))
print(type(factorial))
#print(factorial.__name__)
print(factorial(10))
&#39;&#39;&#39;接下来的是输出结果,通过结果其实很容易发现执行的过程
call performance
call log_decrator 通过观察前两组的输出结果可以知道,先执行装饰器
<type &#39;function&#39;>
call wrapper
call factorial
call factorial() in 0.000000 ms
3628800
&#39;&#39;&#39;
Copy after login

Related recommendations:

Analysis of Python Decorator Principles and Usage

The above is the detailed content of Example analysis of the execution process of Python decorator. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!