python 我不知道python中的__iter__,谁能给我一个好的代码示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1956623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
i don't know __iter__ in python,who can give me a good code example
提问by zjm1126
my code run wrong
我的代码运行错误
class a(object):
def __iter(self):
return 33
b={'a':'aaa','b':'bbb'}
c=a()
print b.itervalues()
print c.itervalues()
Please try to use the code, rather than text, because my English is not very good, thank you
请尽量使用代码,而不是文字,因为我的英文不是很好,谢谢
回答by Alex Martelli
a. Spell it right: not
一个。拼写正确:不是
def __iter(self):
but:
但:
def __iter__(self):
with __before andafter iter.
与__之前和之后iter。
b. Make the body right: not
湾 使身体正确:不是
return 33
but:
但:
yield 33
or return iter([33])
或返回 iter([33])
If you returna value from __iter__, return an iterator (an iterable, as in return [33], is almost as good but not quite...); or else, yield1+ values, making __iter__into a generator function (so it intrinsically returns a generator iterator).
如果你return是一个来自 的值__iter__,则返回一个迭代器(一个可迭代的,如return [33],几乎一样好但不完全......);否则,yield1+ 个值,构成__iter__一个生成器函数(因此它本质上返回一个生成器迭代器)。
c. Call it right: not
C。正确称呼:不是
a().itervalues()
but, e.g.:
但是,例如:
for x in a(): print x
or
或者
print list(a())
itervaluesis a method of dict, and has nothing to do with __iter__.
itervalues是 dict 的一种方法,与__iter__.
If you fix all three (!) mistakes, the code works better;-).
如果您修复所有三个 (!) 错误,代码效果会更好;-)。
回答by Vlad the Impala
A few things about your code:
关于你的代码的一些事情:
__itershould be__iter__- You're returning '33' in the
__iter__function. You should actually be returning an iterator object. An iterator is an object which keeps returning different values when it'snext()function is called (maybe a sequence of values like [0,1,2,3 etc]).
__iter应该__iter__- 您在
__iter__函数中返回“33” 。您实际上应该返回一个迭代器对象。迭代器是一个对象,它在next()调用函数时不断返回不同的值(可能是一系列值,如 [0,1,2,3 等])。
Here's a working example of an iterator:
这是一个迭代器的工作示例:
class a(object):
def __init__(self,x=10):
self.x = x
def __iter__(self):
return self
def next(self):
if self.x > 0:
self.x-=1
return self.x
else:
raise StopIteration
c=a()
for x in c:
print x
Any object of class ais an iterator object. Calling the __iter__function is supposed to return the iterator, so it returns itself– as you can see, the a class has a next()function, so this is an iterator object.
类的任何对象a都是迭代器对象。调用__iter__函数应该返回迭代器,所以它返回自身——如你所见,a 类有一个next()函数,所以这是一个迭代器对象。
When the next function is called, it keeps return consecutive values until it hits zero, and then it sends the StopIterationexception, which (appropriately) stops the iteration.
当下一个函数被调用时,它保持返回连续值直到它达到零,然后它发送StopIteration异常,这(适当地)停止迭代。
If this seems a little hazy, I would suggest experimenting with the code and then checking out the documentation here: http://docs.python.org/library/stdtypes.html
如果这看起来有点模糊,我建议尝试使用代码,然后在此处查看文档:http: //docs.python.org/library/stdtypes.html
回答by Omnifarious
Here is a code example that implements the xrange builtin:
这是一个实现 xrange 内置的代码示例:
class my_xrange(object):
def __init__(self, start, end, skip=1):
self.curval = int(start)
self.lastval = int(end)
self.skip = int(skip)
assert(int(skip) != 0)
def __iter__(self):
return self
def next(self):
if (self.skip > 0) and (self.curval >= self.lastval):
raise StopIteration()
elif (self.skip < 0) and (self.curval <= self.lastval):
raise StopIteration()
else:
oldval = self.curval
self.curval += self.skip
return oldval
for i in my_xrange(0, 10):
print i
回答by wlashell
You are using this language feature incorrectly.
您错误地使用了此语言功能。
http://docs.python.org/library/stdtypes.html#iterator-types
http://docs.python.org/library/stdtypes.html#iterator-types
This above link will explain what the function should be used for.
上面的链接将解释该函数的用途。
You can try to see documentation in your native language here: http://wiki.python.org/moin/Languages
您可以尝试在此处查看您的母语文档:http: //wiki.python.org/moin/Languages

