Naming conventions for iterator methods?

John J. Lee jjl at pobox.com
Tue Dec 23 10:01:33 EST 2003


Peter Otten <__peter__ at web.de> writes:
> John J. Lee wrote:
[...]
> My personal naming convention is not yet stable, but I currently favour
> foos(). I think iterkeys() etc. were only introduced because there already
> were methods like keys() returning the corresponding list.
> These I now tend to omit and instead make it explicit when I need a list, e.
> g.
> 
> alltags = list(parser.tags())

Yeah, I think you're right on both counts.


> and instead of parser.filtertags(pred):
> 
> sometags = [t for t in parser.tags() if pred(t)] 

Yes.  Also, I have keyword arguments to some .foos() methods.


> When all returned sequences are iterators, a special prefix/suffix seems
> pointless and you may wonder how to name the few exceptions:
> 
> list_foo(), foo_list(), listfoo(), foolist(), foos(), ...

But a sequence is not an iterator, IIRC, because it doesn't have a
.next() method.  It just "supports the iterator protocol" by virtue of
having an .__iter__() method that returns an iterator (which has both
.next() and .__iter__()).


John




More information about the Python-list mailing list