yield and generator in python

首先,假设大家都对于pytyhon的List comprehension的使用有了一定经验(它可以用于list,set,和dict哦)

不熟悉的参考介绍: Comprehending Python’s Comprehensions – dbader.org

generator

generator是哦嗯一种返回lazy iterator的特殊类型函数。list comprehensions return full lists, while generator expressions return generators.

如下面代码,分别使用list和generator的构造,但是两者所占据的内存大家却完全不一样。

>>> import sys
>>> nums_squared_lc = [i ** 2 for i in range(10000)]
>>> sys.getsizeof(nums_squared_lc)
87624
>>> nums_squared_gc = (i ** 2 for i in range(10000))
>>> print(sys.getsizeof(nums_squared_gc))
120

 generator functions are a special kind of function that return a lazy iterator. These are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store their contents in memory.

在python中,通过“类似list comprehension”或者yield的使用,我们可以构造generator并调用。

yield

yield的作用介绍如下,当使用generator的特殊方法,比如next()时,generator中的代码会被执行到yield,当经过yield时,python中generator的执行会被中断,函数的当前状态会被保存,直到下一次执行。 

下列函数是yield的一个经典用法。

def infinite_sequence():
    num = 0
    while True:
        yield num
        num += 1

在调用该函数的过程中,每一次,输出的数会增加1,相比于return,yield会保存function当前的状态,参考How to Use Generators and yield in Python – Real Python

On the whole, yield is a fairly simple statement. Its primary job is to control the flow of a generator function in a way that’s similar to return statements. As briefly mentioned above, though, the Python yield statement has a few tricks up its sleeve.

When you call a generator function or use a generator expression, you return a special iterator called a generator. You can assign this generator to a variable in order to use it. When you call special methods on the generator, such as next(), the code within the function is executed up to yield.

When the Python yield statement is hit, the program suspends function execution and returns the yielded value to the caller. (In contrast, return stops function execution completely.) When a function is suspended, the state of that function is saved. This includes any variable bindings local to the generator, the instruction pointer, the internal stack, and any exception handling.

This allows you to resume function execution whenever you call one of the generator’s methods. In this way, all function evaluation picks back up right after yield. You can see this in action by using multiple Python yield statements:

 

 When you use next(), Python calls .__next__() on the function you pass in as a parameter. There are some special effects that this parameterization allows, but it goes beyond the scope of this article. Experiment with changing the parameter you pass to next() and see what happens!

  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值