Home  >  Article  >  Backend Development  >  Detailed explanation of examples of decorators, iterators and generators in Python

Detailed explanation of examples of decorators, iterators and generators in Python

黄舟
黄舟Original
2017-07-26 15:45:02879browse

The editor below will bring you an old-fashioned talk about decorators, iterators and generators in Python. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

When learning python, the three "famous tools" should be considered a small difficulty for people who have no experience in programming in other languages. This blog is about the blogger himself Decorators, iterators, and generator understanding are explained.

Why use decorators

What are decorators? "Decoration" literally means the act of beautifying a specific building according to a certain idea and style. The so-called "device" is a tool. For python, a decorator is a method that can be used without modifying the original code. Add new functions to it. For example, after a software is online, we need to add new functions periodically without modifying the source code or the way it is called. In python, we can use decorators to achieve this. , also when writing code, we must also consider the later scalability. Let’s take a step-by-step look at Python’s decorators.

A simple example introduces a no-argument decorator

Let’s look at a few simple lines of code first. The result of the code is to sleep for 2 seconds. , and then print "hello boy!":


import time
def foo():
 """打印"""
 time.sleep(2)
 print("Hello boy!")
foo()

We now need to add a program timing function to it, but the original code cannot be modified:


import time
def timmer(func):
 def wrapper():
  """计时功能"""
  time_start=time.time()
  func()
  time_end=time.time()
  print("Run time is %f "%(time_end-time_start))
 return wrapper
def foo():
 """打印"""
 time.sleep(2)
 print("Hello boy!")
foo=timmer(foo)
foo()
#运行结果
Hello boy!
Run time is 2.000446

Look! We implemented this function without modifying the original code. Because functions are also objects, we can pass function foo as a parameter to function timmer.

In python, there is a more concise way to replace foo=timmer(foo), using @timmer, which is called syntactic sugar in python.


import time
def timmer(func):
 def wrapper():
  """计时功能"""
  time_start=time.time()
  func()
  time_end=time.time()
  print("Run time is %f "%(time_end-time_start))
 return wrapper
@timmer  #等于 foo=timmer(foo)
def foo():
 """打印"""
 time.sleep(2)
 print("Hello boy!")
foo()

Let’s analyze the execution process of the function step by step:

1. Import the time module


import time

2. Define the function timmer. Defining the function will not execute the code in the function


def timmer(func):

3. Call the decorator, which is equivalent to foo=timer(foo ), that is, passing the function foo as a parameter to the function timmer


@timmer

4. Run the function timmer and accept the parameter func=foo


def timmer(func):

5. In the function timmer, the function wrapper is defined, the code inside the wrapper function is not executed, and then the function wrapper is returned as the return value


return wrapper

6. Assign the return value to foo. In step 3, foo=timmer(foo), remember


@timmer #等于 foo=timmer(foo)

7. Run function foo() , but the function here is no longer the original one. You can print foo. Yes, because we passed wrapper as the return value to foo before, so executing foo here is executing wrapper. To confirm this again, you You can also print the wrapper. Their memory addresses are the same, so they all point to the same address space:


<function timmer.<locals>.wrapper at 0x00000180E0A8A950> #打印foo的结果
<function timmer.<locals>.wrapper at 0x000001F10AD8A950> #打印wrapper的结果
foo()

8. Run the function wrapper, record the start time, and execute the function func. In step 4, func is assigned a value by foo. Running func is running the original function foo, sleeping for 2 seconds, and printing a string;


time_start=time.time()
 time.sleep(2)
 print("Hello boy!")

9. Record the end time , print the running time, and the program ends.


Hello boy!
Run time is 2.000161

Decorator with parameters

In the previous example, the original function has no parameters, as follows Let’s take a look at how to modify the decorator function when the original function has parameters?


import time
def timmer(func):
 def wrapper(*args,**kwargs):
  """计时功能"""
  start_time=time.time()
  res=func(*args,**kwargs)
  end_time=time.time()
  print("Run time is %f"%(end_time-start_time))
  return res
 return wrapper
@timmer 
def my_max(x,y):
 """返回两个值的最大值"""
 res=x if x > y else y
 time.sleep(2)
 return res
res=my_max(1,2)
print(res)
#运行结果
Run time is 2.000175

When the original function needs to pass in parameters, in this example my_max has two positions where parameters need to be passed in. You only need to add two shapes on the wrapper. Parameters. It is also possible to use variable parameters (*args, **kwargs) in this example. This is @timmer which is equal to my_max(1,2)=timmer(my_max)

Let’s look at one below Decorator with parameters:


def auth(filetype):
 def auth2(func):
  def wrapper(*args,**kwargs):
   if filetype == "file":
    username=input("Please input your username:")
    passwd=input("Please input your password:")
    if passwd == &#39;123456&#39; and username == &#39;Frank&#39;:
     print("Login successful")
     func()
    else:
     print("login error!")
   if filetype == &#39;SQL&#39;:
    print("No SQL")
  return wrapper
 return auth2
@auth(filetype=&#39;file&#39;) #先先返回一个auth2 ==》@auth2 ==》 index=auth2(index) ==》 index=wrapper
def index():
 print("Welcome to China")
index()

If the decorator itself has parameters, an additional layer of inline functions is needed. , let’s analyze the execution process step by step:

1. Define function auth


def auth(filetype):

2. To call the interpreter, first run the function auth(filetype= 'file')


@auth(filetype=&#39;file&#39;)

3. Run the function auth, define a function auth2, and return it as the return value, then this @auth(filetype='file') Equivalent to @auth2, equivalent to index=auth2(index)


def auth(filetype):
 def auth2(func):
  def wrapper(*args,**kwargs):
  return wrapper
 return auth2

4.auth2(index) execution, func=index, define function wrapper, and return In short, at this time index is actually equal to wrapper


def wrapper(*args,**kwargs):
return wrapper

5. When running index, that is, running wrapper, running the internal code of the function, filetype=="file", prompting the user Output the user name and password and determine whether the input is correct. If correct, execute the function func(), which is equivalent to executing the original index and printing


if filetype == "file":
    username=input("Please input your username:")
    passwd=input("Please input your password:")
    if passwd == &#39;123456&#39; and username == &#39;Frank&#39;:
     print("Login successful")
     func()

6. Run the result test


Please input your username:Frank
Please input your password:123456
Login successful
Welcome to China

装饰器也是可以被叠加的:


import time
#
def timmer(func):
 def wrapper():
  """计时功能"""
  time_start=time.time()
  func()
  time_end=time.time()
  print("Run time is %f "%(time_end-time_start))
  # print("---",wrapper)
 return wrapper
def auth(filetype):
 def auth2(func):
  def wrapper(*args,**kwargs):
   if filetype == "file":
    username=input("Please input your username:")
    passwd=input("Please input your password:")
    if passwd == &#39;123456&#39; and username == &#39;Frank&#39;:
     print("Login successful")
     func()
    else:
     print("login error!")
   if filetype == 'SQL':
    print("No SQL")
  return wrapper
 return auth2
@timmer
@auth(filetype=&#39;file&#39;) #先先返回一个auth2 ==》@auth2 ==》 index=auth2() ==》 index=wrapper
def index():
 print("Welcome to China")
index()

#测试结果
Please input your username:Frank
Please input your password:123456
Login successful
Welcome to China
Run time is 7.966267

注释优化


import time
def timmer(func):
 def wrapper():
  """计算程序运行时间"""
  start_time=time.time()
  func()
  end_time=time.time()
  print("Run time is %s:"%(end_time-start_time))
 return wrapper
@timmer
def my_index():
 """打印欢迎"""
 time.sleep(1)
 print("Welcome to China!")
my_index()
print(my_index.__doc__)

#运行结果
Welcome to China!
Run time is 1.0005640983581543:
计算程序运行时间

当我们使用了装饰器的时候,虽然没有修改代码本身,但是在运行的时候,比如上面这个例子,运行my_index其实在运行wrapper了,如果我们打印my_index的注释信息,会打印wrapper()的注释信息,那么该怎么优化?

可以在模块functools中导入wraps,具体见以下:


import time
from functools import wraps
def timmer(func):
 @wraps(func)
 def wrapper():
  """计算程序运行时间"""
  start_time=time.time()
  func()
  end_time=time.time()
  print("Run time is %s:"%(end_time-start_time))
 return wrapper
@timmer
def my_index():
 """打印欢迎"""
 time.sleep(1)
 print("Welcome to China!")
my_index()
print(my_index.__doc__)
#运行结果
Welcome to China!
Run time is 1.0003223419189453:
打印欢迎

这样,在表面看来,原函数没有发生任何变化。

为什么要用迭代器

从字面意思,迭代就是重复反馈过程的活动,其目的通常是为了比较所需目标或结果,在python中可以用迭代器来实现,先来描述一下迭代器的优缺点,如果看不懂可以先略过,等看完本博客再回头看,相信你会理解其中的意思:

优点:

迭代器在取值的时候是不依赖于索引的,这样就可以遍历那些没有索引的对象,比如字典和文件

迭代器与列表相比,迭代器是惰性计算,更节省内存

缺点:

无法获取迭代器的长度,没有列表灵活

只能往后取值,不能倒着取值

什么是迭代器

那么在python什么才算是迭代器呢?

只要对象有__iter__(),那么它就是可迭代的,迭代器可以使用函数next()来取值

下面我们来看一个简单的迭代器:


my_list=[1,2,3]
li=iter(my_list)  #li=my_list.__iter__()
print(li)
print(next(li))
print(next(li))
print(next(li))
#运行结果
<list_iterator object at 0x000002591652C470>
2

可以看到,使用内置函数iter可以将列表转换成一个列表迭代器,使用next()获取值,一次值取一个值,当值取完了,再使用一次next()的时候,会报异常StopIteration,可以通过异常处理的方式来避免,try-except-else就是一个最常用的异常处理结构:


my_list=[1,2,3]
li=iter(my_list)
while True:
 try:
  print(next(li))
 except StopIteration:
  print("Over")
  break
 else:
  print("get!")
#运行结果
get!
get!
get!
Over

查看可迭代对象和迭代器对象

使用Iterable模块可以判断对象是否是可迭代的:


from collections import Iterable
s="hello" #定义字符串
l=[1,2,3,4] #定义列表
t=(1,2,3) #定义元组
d={&#39;a&#39;:1} #定义字典
set1={1,2,3,4} #定义集合
f=open("a.txt") #定义文本
# 查看是否都是可迭代的
print(isinstance(s,Iterable))
print(isinstance(l,Iterable))
print(isinstance(t,Iterable))
print(isinstance(d,Iterable))
print(isinstance(set1,Iterable))
print(isinstance(f,Iterable))
#运行结果
True
True
True
True
True
True

通过判断,可以确定我们所知道的常用的数据类型都是可以被迭代的。

使用Iterator模块可以判断对象是否是迭代器:


from collections import Iterable,Iterator
s="hello"
l=[1,2,3,4]
t=(1,2,3)
d={&#39;a&#39;:1}
set1={1,2,3,4}
f=open("a.txt")
# 查看是否都是可迭代的
print(isinstance(s,Iterator))
print(isinstance(l,Iterator))
print(isinstance(t,Iterator))
print(isinstance(d,Iterator))
print(isinstance(set1,Iterator))
print(isinstance(f,Iterator))
#运行结果
False
False
False
False
False
True

可知只有文件是迭代器,所以可以直接使用next(),而不需要转换成迭代器。

什么是生成器

生产器就是一个是带有yield的函数

下面来看一个简单的生成器


def my_yield():
 print(&#39;first&#39;)
 yield 1
g=my_yield()
print(g)
#运行结果
<generator object my_yield at 0x0000024366D7E258>

生成器也是一个迭代器


from collections import Iterator
def my_yield():
 print(&#39;first&#39;)
 yield 1
g=my_yield()
print(isinstance(g,Iterator))
#运行结果
True

那就可以用next()来取值了


print(next(g))
#运行结果
first
1

生成器的执行过程

我们来看以下下面这个例子,了解生产的执行流程


def my_yield():
 print(&#39;first&#39;)
 yield 1
 print(&#39;second&#39;)
 yield 2
 print(&#39;Third&#39;)
 yield 3
g=my_yield()
next(g)
next(g)
next(g)
#运行结果
first
second
Third

1.定义生成器my_yield,并将其赋值给了g


def my_yield():
g=my_yield()

2.开始第一次执行next(),开始执行生产器函数 ,打印第一语句,遇到yileld的时候暂停,并返回一个1,如果你想打印返回值的话,这里会显示1


 print(&#39;first&#39;)
 yield 1

3.再执行2次,打印字符串(每执行一次都会暂停一下)


 print(&#39;second&#39;)
 yield 2
 print(&#39;Third&#39;)
 yield 3

4.如果再加一次next()就会报出StopIteration异常了

生成器在每次暂停的时候,函数的状态将被保存下来,来看下面的例子:


def foo():
 i=0
 while True:
  yield i
  i+=1
g=foo()
for num in g:
 if num < 10:
  print(num)
 else:
  break
#运行结果

for循环中隐含next(),每next一次,暂停一次,if语句判断一次,然后执行下一次next,可以看到我们的while循环并没有无限循环下去,而是状态被保存下来了。

协程函数

我们来看下面这个生成器和执行结果


def eater(name):
 print(&#39;%s start to eat food&#39;%name)
 while True:
  food=yield
  print(&#39;%s get %s ,to start eat&#39;%(name,food))
 print(&#39;done&#39;)
e=eater(&#39;Frank&#39;)
next(e)
e.send(&#39;egg&#39;) #给yield送一个值,并继续执行代码
e.send(&#39;tomato&#39;)
#运行结果
Frank start to eat food
Frank get egg ,to start eat
Frank get tomato ,to start eat

send可直接以向yield传值,含有yield表达式的函数我们也称为协程函数,

这运行程序的时候,不可以直接send,必须先使用next()初始化生成器。

如果存在多个这样的函数,那么我们每次执行的时候都要去next()一下,为了防止忘记这一步操作,可以使用装饰器初始化:


def init(func):
 def wrapper(*args):
  res = func(*args)
  next(res)  # 在这里执行next
  return res
 return wrapper
@init
def eater(name):
 print(&#39;%s start to eat food&#39;%name)
 while True:
  food=yield
  print(&#39;%s get %s ,to start eat&#39;%(name,food))
 print(&#39;done&#39;)
e=eater(&#39;Frank&#39;)
e.send(&#39;egg&#39;) 
e.send(&#39;tomato&#39;)

所以在程序中有更多的生成器需要初始化的时候,直接调用这个装饰器就可以了。

The above is the detailed content of Detailed explanation of examples of decorators, iterators and generators in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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