Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34979 > unrolled thread

Re: Supporting list()

Started byEthan Furman <ethan@stoneleaf.us>
First post2012-12-17 15:30 -0800
Last post2012-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.


Contents

  Re: Supporting list() Ethan Furman <ethan@stoneleaf.us> - 2012-12-17 15:30 -0800

#34979 — Re: Supporting list()

FromEthan Furman <ethan@stoneleaf.us>
Date2012-12-17 15:30 -0800
SubjectRe: 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~

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web