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


Groups > comp.lang.python > #34979

Re: Supporting list()

Date 2012-12-17 15:30 -0800
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: Supporting list()
References <loom.20121217T152936-30@post.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.960.1355758228.29569.python-list@python.org> (permalink)

Show all headers | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web