Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python是否提供;“免费”;默认迭代器?_Python_Iterator_Default - Fatal编程技术网

Python是否提供;“免费”;默认迭代器?

Python是否提供;“免费”;默认迭代器?,python,iterator,default,Python,Iterator,Default,可能重复: 我有一个类,它基本上是python列表的包装器。在这个类中,我定义了\uuu getitem\uuuuuuuuuuuuuu,\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu setitem和。我还没有为这个类定义\uuuuuuuuuuuuuuuuuuuuuuuuuu 当我走的时候: thing = ListWrapper(range(4)) for i in thing : print i 我得到输出:

可能重复:

我有一个类,它基本上是python列表的包装器。在这个类中,我定义了
\uuu getitem\uuuuuuuuuuuuuu
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu setitem
。我还没有为这个类定义
\uuuuuuuuuuuuuuuuuuuuuuuuuu

当我走的时候:

thing = ListWrapper(range(4))
for i in thing :
    print i
我得到输出:

0
1
2
3
这很好,但我预期会出现某种错误消息,说python找不到迭代器。我查看了文档,没有找到任何引用默认迭代器的内容。此外,通过PyDev中的代码跟踪显示,它在每次迭代中调用
\uuuu getitem\uuuu
方法


我想知道在我的代码中依赖这种行为是否是一种好的做法。在这一点上我觉得不太对。Python是否保证具有
\uuuu getitem\uuuuuu
\uuuu len\uuuuu
的类将被视为具有定义的迭代器?如果一个类没有
\uuuu iter\uuuu
,但是有
\uu getitem\uuuuu
,迭代机制会用连续整数调用它,直到它用完为止。

@Ned谢谢,这就是我一直在寻找的,但找不到的。啊,搜索词的挑剔本质。