python make class iterable by returning embedded iterable

被刻印的时光 ゝ 提交于 2019-12-05 00:10:19

The "best" way to way to delegate __iter__ would be:

def __iter__(self):
    return iter(self._iterable)

Alternately, it might be worth knowing about:

def __iter__(self):
    for item in self._iterable:
        yield item

Which will let you fiddle with each item before returning it (ex, if you wanted yield item * 2).

And as @Lattyware mentions in the comments, PEP380 (slated for inclusion in Python 3.3) will allow:

def __iter__(self):
    yield from self._iterable

Note that it may be tempting to do something like:

def __init__(self, iterable):
    self.__iter__ = iterable.__iter__

But this won't work: iter(foo) calls the __iter__ method on type(foo) directly, bypassing foo.__iter__. Consider, for example:

class SurprisingIter(object):
    def __init__(self):
        self.__iter__ = lambda self: iter("abc")

    def __iter__(self):
        return iter([1, 2, 3])

You would expect that list(SurprisingIter()) would return ["a", "b", "c"], but it actually returns [1, 2, 3].

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!