Re: Supporting list()

Discussion in 'Python' started by Dave Angel, Dec 17, 2012.

  1. Dave Angel

    Dave Angel Guest

    On 12/17/2012 06:27 PM, Ethan Furman wrote:
    > Dave Angel wrote:
    >> On 12/17/2012 09:33 AM, Skip Montanaro wrote:
    >>> What method(s) does a class have to support to properly emulate a
    >>> container
    >>> which supports turning it into a list? For example:
    >>>
    >>> class Foo:
    >>> pass
    >>>
    >>> f = Foo()
    >>> print list(f)
    >>>
    >>> Is it just __iter__() and next()? (I'm still using 2.4 and 2.7.)

    >>
    >> I believe the container class needs to define the __iter__() method,
    >> which has to return an iterator object.
    >>
    >> That (possibly different) iterator class needs both an __iter__() method
    >> and a next() method.
    >>
    >> If the container class is also the iterator class, which is common, then
    >> you just need one __iter__() method, which returns self.

    >
    > The `next()` method is also needed, as `__iter__()` and `next()` are the
    > two methods that make up the iterator protocol (`__next__` in python 3k).
    >
    > ~Ethan~
    >
    >


    Didn't I say that? The next() method need not be in the container
    class; it needs to be in the iterator class returned by the __iter__()
    method.

    class MyIter():
    def __init__(self, value):
    self.internal = value
    def __iter__(self):
    return self
    def next(self):
    self.internal += 1
    if self.internal > 100:
    raise StopIteration
    return self.internal


    class Container:
    def __iter__(self):
    return MyIter(42)


    for item in Container():
    print item

    print list(Container())

    (tested in Python 2.7)


    AHH, upon rereading, I see you misinterpreted what I meant. I was
    trying to say that if there was only one class serving as both container
    and iterator, you only needed one of the __iter__() methods instead of
    two. In other words, you need two methods, not three.

    --

    DaveA
     
    Dave Angel, Dec 17, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lin Chat
    Replies:
    3
    Views:
    107
    Huw Collingbourne
    May 30, 2008
  2. Skip Montanaro

    Supporting list()

    Skip Montanaro, Dec 17, 2012, in forum: Python
    Replies:
    0
    Views:
    148
    Skip Montanaro
    Dec 17, 2012
  3. Dave Angel

    Re: Supporting list()

    Dave Angel, Dec 17, 2012, in forum: Python
    Replies:
    0
    Views:
    154
    Dave Angel
    Dec 17, 2012
  4. Ethan Furman

    Re: Supporting list()

    Ethan Furman, Dec 17, 2012, in forum: Python
    Replies:
    0
    Views:
    186
    Ethan Furman
    Dec 17, 2012
  5. Skip Montanaro

    Re: Supporting list()

    Skip Montanaro, Dec 17, 2012, in forum: Python
    Replies:
    0
    Views:
    153
    Skip Montanaro
    Dec 17, 2012
Loading...

Share This Page