Naming conventions for iterator methods?

François Pinard pinard at iro.umontreal.ca
Mon Dec 22 21:16:42 EST 2003


[John J. Lee]
> How do people name their iterator methods / functions?

> eg. .iter_foo(), .foo_iter(), .iterfoo(), .fooiter(), .foos(), ...?

I took the habit of naming such methods and functions with names
starting with `all_', so I can write:

    for name in all_names():
       do something

or maybe:

    tokens = all_tokens()
    ...
    token = tokens.next()
    ...

I find this more legible than forcing the string `iter' here and there,
and leaves a bit more freedom to change the code later -- let's say, if
I ever want to substitute a solid list for the iterator.  I can keep the
`all_' prefix, while I would feel uncomfortable it were some `iter' in
the names.  Granted, if I want to use `.next()', I'll have to plunk one
more `iter()' in the code.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard





More information about the Python-list mailing list