Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #34984 > unrolled thread
| Started by | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| First post | 2012-12-17 15:27 -0800 |
| Last post | 2012-12-17 15:27 -0800 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Supporting list() Ethan Furman <ethan@stoneleaf.us> - 2012-12-17 15:27 -0800
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2012-12-17 15:27 -0800 |
| Subject | Re: Supporting list() |
| Message-ID | <mailman.965.1355759624.29569.python-list@python.org> |
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~
Back to top | Article view | comp.lang.python
csiph-web