Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #34979 > unrolled thread
| Started by | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| First post | 2012-12-17 15:30 -0800 |
| Last post | 2012-12-17 15:30 -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:30 -0800
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2012-12-17 15:30 -0800 |
| Subject | Re: Supporting list() |
| Message-ID | <mailman.960.1355758228.29569.python-list@python.org> |
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.) You can either use __iter__ and next to conform to the iterator protocol, or you can define __getitem__. If using __getitem__ it needs to work with integers from 0 to len(f)-1, and raise IndexError for len(f), len(f+1), etc. ~Ethan~
Back to top | Article view | comp.lang.python
csiph-web