Python 3.x:测试生成器是否有剩余元素 [英] Python 3.x: Test if generator has elements remaining
问题描述
当我在 for 循环中使用生成器时,它似乎知道",当没有更多元素产生时.现在,我必须使用没有 for 循环的生成器,并手动使用 next() 来获取下一个元素.我的问题是,我怎么知道是否没有更多元素?
我只知道:next() 引发异常(StopIteration),如果什么都没有,那么对于这样一个简单的问题,BUT 是不是有点太重"了?难道没有has_next()之类的方法吗?
以下几行应该清楚我的意思:
#!/usr/bin/python3# 定义一些对象的列表bar = ['abc',123,无,真,456.789]# 我们的原始生成器定义富(酒吧):对于 bar 中的 b:产量 b# 迭代,使用上面的生成器print('--- TEST A (for 循环) ---')对于 foo(bar) 中的 baz:打印(巴兹)打印()# 为变量分配一个新的迭代器foobar = foo(bar)print('--- TEST B (try-except) ---')而真:尝试:打印(foobar.__next__())除了停止迭代:休息打印()# 为变量分配一个新的迭代器foobar = foo(bar)# 显示生成器成员print('--- 生成器成员 ---')print(', '.join(dir(foobar)))
输出如下:
--- TEST A(for循环)---美国广播公司123没有任何真的456.789--- 测试 B (try-except) ---美国广播公司123没有任何真的456.789--- 发电机成员 ---__class__,__delattr__,__doc__,__eq__,__format__,__ge__,__getattribute__,__gt__,__hash__,__init__,__iter__,__le__,__lt__,__name__,__ne__,__new__,__next__,__reduce__,__reduce_ex__,__repr__,__setattr__,__subsizeof__,gi_code、gi_frame、gi_running、发送、抛出
感谢大家,祝您有美好的一天!:)
您编写的两个语句以完全相同的方式处理查找生成器的末尾.for 循环只是调用 .next() 直到引发 StopIteration 异常然后终止.
http://docs.python.org/tutorial/classes.html#iteratorsp>
因此,我不认为等待 StopIteration 异常是处理问题的繁重"方式,而是生成器的设计使用方式.
When I use a generator in a for loop, it seems to "know", when there are no more elements yielded. Now, I have to use a generator WITHOUT a for loop, and use next() by hand, to get the next element. My problem is, how do I know, if there are no more elements?
I know only: next() raises an exception (StopIteration), if there is nothing left, BUT isn't an exception a little bit too "heavy" for such a simple problem? Isn't there a method like has_next() or so?
The following lines should make clear, what I mean:
#!/usr/bin/python3
# define a list of some objects
bar = ['abc', 123, None, True, 456.789]
# our primitive generator
def foo(bar):
for b in bar:
yield b
# iterate, using the generator above
print('--- TEST A (for loop) ---')
for baz in foo(bar):
print(baz)
print()
# assign a new iterator to a variable
foobar = foo(bar)
print('--- TEST B (try-except) ---')
while True:
try:
print(foobar.__next__())
except StopIteration:
break
print()
# assign a new iterator to a variable
foobar = foo(bar)
# display generator members
print('--- GENERATOR MEMBERS ---')
print(', '.join(dir(foobar)))
The output is as follows:
--- TEST A (for loop) ---
abc
123
None
True
456.789
--- TEST B (try-except) ---
abc
123
None
True
456.789
--- GENERATOR MEMBERS ---
__class__, __delattr__, __doc__, __eq__, __format__, __ge__, __getattribute__, __gt__, __hash__, __init__, __iter__, __le__, __lt__, __name__, __ne__, __new__, __next__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__, close, gi_code, gi_frame, gi_running, send, throw
Thanks to everybody, and have a nice day! :)
The two statements you wrote deal with finding the end of the generator in exactly the same way. The for-loop simply calls .next() until the StopIteration exception is raised and then it terminates.
http://docs.python.org/tutorial/classes.html#iterators
As such I don't think waiting for the StopIteration exception is a 'heavy' way to deal with the problem, it's the way that generators are designed to be used.
这篇关于Python 3.x:测试生成器是否有剩余元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!