
在这篇文章中,我们将向您解释什么是Python中的生成器和装饰器。
自从 PEP 255 引入生成器以来,它们一直是 Python 的重要组成部分。
Python 中的生成器是一个特殊的例程,可用于控制循环的迭代行为。生成器类似于返回数组的函数。生成器有一个参数,我们可以调用它并生成一个数字序列。但与返回整个数组的函数不同,生成器一次生成一个值,需要更少的内存。
任何带有关键字“yield”的Python函数都可以称为生成器。普通的 python 函数从第一行开始执行,并继续执行,直到我们收到 return 语句或异常或函数结束,但是,在函数作用域期间创建的任何局部变量都将被销毁,并且无法进一步访问。而对于生成器来说,当它遇到yield关键字时,函数的状态将被冻结,并且所有变量都将存储在内存中,直到再次调用生成器。
立即学习“Python免费学习笔记(深入)”;
我们可以根据迭代器使用生成器,也可以使用“next”关键字显式调用。
通常是 Python 中的生成器 -
使用 def 关键字定义
使用yield关键字
可能包含多个收益关键字。
返回一个迭代器。
生成器是返回可迭代生成器对象的函数。由于生成器对象中的值是一次获取一个值,而不是一次获取整个列表,因此您可以使用 for 循环、next() 或 list() 函数来获取实际值。
可以使用生成器函数和生成器表达式创建生成器。
生成器函数与常规函数类似,但它没有返回值,而是具有yield关键字。
要创建生成器函数,请添加 yield 关键字。下面的示例演示了如何编写生成器函数。
带有迭代器的生成器
# creating a function def generatorExample(): yield "T" yield "U" yield "T" yield "O" yield "R" yield "I" yield "A" yield "L" yield "S" # calling the generatorExample() function which is created above result = generatorExample() # Traversing in the above result(generator object) for k in result: # Printing the corresponding value print(k)
T U T O R I A L S
从生成器读取产量值
list()、for-loop 和 next() 方法可用于从生成器对象读取值。
next() 方法返回列表、数组或对象中的下一项。当列表为空并且调用 next() 时,它会返回一个带有 stopIteration 信号的错误。此错误表明列表中没有更多条目。
# creating a function that accepts a number as an argument
def oddNumbers(num):
# traversing till that number passed
for i in range(num):
# checking whether the iterator index value is an odd number
if (i%2!=0):
# getting the iterator index value if the condition is true using the yield keyword
yield i
# calling the above function to get the odd numbers below 8
result = oddNumbers(8)
# calling the next items in the result list
print(next(result))
print(next(result))
print(next(result))
print(next(result))
# throws an error since the list has no more elements
print(next(result))
1
3
5
7
Traceback (most recent call last):
File "main.py", line 17, in <module>
print(next(result))
StopIteration
Python 提供了一个名为装饰器的神奇工具,用于向现有代码添加功能。
这也称为元编程,因为程序的一部分尝试在编译时修改程序的另一部分。
装饰器使用函数作为另一个函数中的参数,然后在包装函数内调用该函数。
@tutorials_decorator
def python_decorator():
print("Hello tutorials Point")
'''Above code is equivalent to -
def python_decorator():
print("Hello tutorials Point")
python_decorator = tutorials_decorator(python_decorator)'''
这里的tutorials_decorator是一个可调用函数,它在另一个可调用函数python_decorator之上添加一些代码并返回包装函数。 p>
这里func是被装饰的函数,python_decorator是用来装饰它的函数
# defining a decorator
def python_decorator(func):
def wrapper():
print("Text before calling the function")
func()
print("Text after calling the function")
return wrapper
def tutorials_decorator():
print("Hello tutorials Point!!!")
tutorials_decorator = python_decorator(tutorials_decorator)
tutorials_decorator()
Text before calling the function Hello tutorials Point!!! Text after calling the function
python_decorator(func) - 这是一个装饰器函数;它接受另一个函数作为参数并“装饰”它,这意味着它修改它并返回修改后的版本。
wrapper - 我们在装饰器函数中定义了另一个名为 wrapper 的内部函数。这是通过包装来修改传递的函数 func 的实际函数。
包装函数由装饰器返回。
tutorials_decorator - 这是我们需要装饰的普通函数。这里只打印一个简单的语句。
上面描述的装饰器模式在Python社区中流行起来,但它有点复杂。我们必须将函数名写三遍,并且修饰隐藏在函数定义下面。
因此,Python 添加了一种使用装饰器的新方法,即通过使用 @ 符号 包含语法糖。
@decorator def func(arg1, arg2, ...): pass
语法糖是编程语言中使用的语法,使内容更易于阅读或表达。
以下示例执行与前一个示例相同的操作 -
# defining a decorator
def python_decorator(func):
def wrapper():
print("Text before calling the function")
func()
print("Text after calling the function")
return wrapper
@python_decorator
def tutorials_decorator():
print("Hello tutorials Point!!!")
tutorials_decorator()
Text before calling the function Hello tutorials Point!!! Text after calling the function
与前面的示例相同,唯一的区别是我们使用 @python_decorator 而不是
tutorials_decorator = python_decorator(tutorials_decorator)
在本文中,我们简要了解了 Python 中的生成器和装饰器。我们还演示了如何在编写代码时使用生成器和装饰器。
以上就是生成器和装饰器在Python中是什么?的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号