什么使迭代器成为迭代器? [英] What makes an iterator an iterator?
问题描述
我认为迭代器是跟随迭代器
协议的任何对象,也就是说,它有一个next()方法和一个__iter __()方法。
但是我在写一个充当迭代器的类时遇到了问题。我有:
类Parrot(对象):
def __iter __(自我):
返回自我
def __init __(自我):
self.next = self._next()
def _next(self):
for word在Norwegian Blue中有漂亮的羽毛!。split():
产生的话
但这就是我得到的:
I thought that an iterator was any object that follows the iterator
protocol, that is, it has a next() method and an __iter__() method.
But I''m having problems writing a class that acts as an iterator. I have:
class Parrot(object):
def __iter__(self):
return self
def __init__(self):
self.next = self._next()
def _next(self):
for word in "Norwegian Blue''s have beautiful plumage!".split():
yield word
But this is what I get:
>> P = Parrot()
用于P中的单词:
>>P = Parrot()
for word in P:
....打印字
....
Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在< module>中
TypeError:iter()返回非迭代器输入''Parrot''
为什么我的实例不是迭代器?
但我可以这样做:
.... print word
....
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: iter() returned non-iterator of type ''Parrot''
Why is my instance not an iterator?
But I can do this:
>> for P.next中的单词:
>>for word in P.next:
....打印单词
。 ...
挪威语
Blue''s
有
漂亮
羽毛!
我发现自己对这种行为感到困惑。
-
Steven D''Aprano
.... print word
....
Norwegian
Blue''s
have
beautiful
plumage!
I find myself perplexed as to this behaviour.
--
Steven D''Aprano
推荐答案
2007年4月18日星期三15:39:22 +1000,Steven D''Aprano写道:
On Wed, 18 Apr 2007 15:39:22 +1000, Steven D''Aprano wrote:
我认为迭代器是跟随迭代器
协议的任何对象,也就是说,它有一个next()方法和一个__iter __()方法。
I thought that an iterator was any object that follows the iterator
protocol, that is, it has a next() method and an __iter__() method.
....
....
class Parrot(object):
class Parrot(object):
....
....
def __init __(self):
self.next = self._next( )
def __init__(self):
self.next = self._next()
self.next这里不是方法,它是一个发电机。你可以这样做:
def __init __(自我):
self.next = self._next()。next
但是,经过测试,这似乎也不起作用 - 我认为
这是因为,再次,self.next不是严格意义上的方法,它是一个
属性。
python手册为您提供了一个可能的解决方案:
--- QUOTE http://docs.python.org/lib/typeiter.html ---
Python的生成器提供了一种实现迭代器
协议的便捷方式。如果容器对象的__iter __()方法实现为
生成器,它将自动返回一个迭代器对象(技术上,一个
生成器对象),提供__iter__ ()和next()方法。
--- END QUOTE ---
即,只需将_next函数重命名为__iter__即可。你的类不会是
本身是一个迭代器,但是它可用于语句中,因此可以转换为带有iter内置函数的迭代器。>
br />
self.next isn''t a method here, it''s a generator. You could do:
def __init__(self):
self.next = self._next().next
But, having tested, that doesn''t appear to work either - I think
this is because, again, self.next is not strictly a method, it''s an
attribute that happens to be callable.
The python manual gives you a possible solution:
---QUOTE http://docs.python.org/lib/typeiter.html ---
Python''s generators provide a convenient way to implement the iterator
protocol. If a container object''s __iter__() method is implemented as a
generator, it will automatically return an iterator object (technically, a
generator object) supplying the __iter__() and next() methods.
---END QUOTE---
i.e., just rename your _next function to __iter__ . Your class won''t
itself be an iterator, but it will be usable in for statements and so one,
and convertable to an iterator with the iter builtin.
Steven D''Aprano< st *** @ REMOVEME.cybersource.com.auwrites:
Steven D''Aprano <st***@REMOVEME.cybersource.com.auwrites:
class Parrot(对象):
def __iter __(自我):
返回自我
def __init __(自我):
self.next = self._next()
def _next(self):
forNorwegian Blue中的单词有漂亮的羽毛!" ;. split():
产生字
class Parrot(object):
def __iter__(self):
return self
def __init__(self):
self.next = self._next()
def _next(self):
for word in "Norwegian Blue''s have beautiful plumage!".split():
yield word
显然问题是你误用了撇号。 Python并没有像复数这样的撇号那样
。
< URL:http://www.angryflower.com/bobsqu.gif>
-
\语音位于思想与|
` \动作之间,位置便利经常替代两者。 - 约翰安德鲁|
_o__)福尔摩斯,_小剂量的_Wisdom_ |
Ben Finney
Clearly the problem is you''ve misused an apostrophe. Python doesn''t
like the plural getting an apostrophe.
<URL:http://www.angryflower.com/bobsqu.gif>
--
\ "Speech is conveniently located midway between thought and |
`\ action, where it often substitutes for both." -- John Andrew |
_o__) Holmes, _Wisdom in Small Doses_ |
Ben Finney
于18.04。 2007 07:39 Steven D''Aprano说如下:
on 18.04.2007 07:39 Steven D''Aprano said the following:
我认为迭代器是跟在迭代器之后的任何对象
I thought that an iterator was any object that follows the iterator
用类的实例替换对象,即相关的方法是在__class__中查找的
而不是实例(我认为)。
我有同样的麻烦试图动态重新分配__call__方法...
replace object with "instance of a class", i.e. the relevant methods are
looked up in the __class__ not in the instance (I think).
I had the same troubles trying to dynamically reassign a __call__ method...
协议,即它有一个next()方法和一个__iter __( )方法。
但是我在写一个充当迭代器的类时遇到了问题。我有:
类Parrot(对象):
def __iter __(自我):
返回自我
def __init __(自我):
self.next = self._next()
def _next(self):
for word在Norwegian Blue中有漂亮的羽毛!" .split():
产量字
protocol, that is, it has a next() method and an __iter__() method.
But I''m having problems writing a class that acts as an iterator. I have:
class Parrot(object):
def __iter__(self):
return self
def __init__(self):
self.next = self._next()
def _next(self):
for word in "Norwegian Blue''s have beautiful plumage!".split():
yield word
试试这个:
类Parrot(对象):
def __iter __(自我):
返回自我
def __init __(self):
self.__class__.next = self._next()。next#see by post
def _next(self):
forNorwegian Blue中的单词有漂亮的羽毛!" .split():
单词
这样可行但实际上迫使班级被用作单身人士...
不是很有帮助:)
更好:
*使用''__iter__返回/是生成器'的方式,
*或者如果你需要对象是迭代器,直接在类上实现下一个
方法::
类Parrot(对象):
def _next(自我):
单词Norwegian Blue中的单词有漂亮的羽毛!。split():
单词
def __iter __(self):
self.generator = self._next()
返回自我
def next(self):
返回self.generator.next()
欢呼,
stefan
Try this::
class Parrot(object):
def __iter__(self):
return self
def __init__(self):
self.__class__.next = self._next().next # see post by I V
def _next(self):
for word in "Norwegian Blue''s have beautiful plumage!".split():
yield word
This works but practically forces the class to be used as a singleton...
not very helpful :)
Better:
* use the ''__iter__ returns/is a generator'' way,
* or if you need the object to be the iterator, implement the next
method directly on the class::
class Parrot(object):
def _next(self):
for word in "Norwegian Blue''s have beautiful plumage!".split():
yield word
def __iter__(self):
self.generator = self._next()
return self
def next(self):
return self.generator.next()
cheers,
stefan
这篇关于什么使迭代器成为迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!