ethan at stoneleaf.us
Tue Dec 18 00:30:27 CET 2012
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:
> 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.
More information about the Python-list