Naming conventions for iterator methods?

François Pinard pinard at
Tue Dec 23 15:46:35 CET 2003

[John J Lee]

> I think I'll go with .foos(), for the same reason you choose .all_foos()
> -- it'll make people read the docs if they need to know whether it's a
> sequence, iterator, or object supporting the iterator protocol.

I surely thought about using a suffix `s'.  I do not know how frequent
the problem is in English, but when I write in-house Python, I use
French identifiers[1], and many words already have an `s' suffix in the
singular form, the spelling does not change when pluralised.

> [François Pinard]

> > 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

> I don't like that, because often my iterator-returning methods take
> arguments to restrict the iterees: shop.iter_cheeses(variety="smelly").

In my humble opinion,

        for cheese in shop.all_cheeses(variety="smelly"):
           do something

would still be quite legible, and not shocking at all.  In French,
`all_' would become `tous_' or `toutes_', of course.

> I just realised some of these methods of mine don't actually return
> iterators, but merely objects supporting the iterator protocol.

It may be a worth idea systematically returning iterables instead of
iterators.  Yet, I think an iterator shall formally provide itself
through its `__iter__' method.

In practice, I noticed that most of my `all_' functions or methods are
based on generators.  So clean! :-) These are very useful.

[1] I mean, nearly French, as the necessary diacritics may not be
used in Python identifiers, which is a common and constant source of
displeasure to those of us who especially like working in French.  Oh,
Python does not differ on this aspect than most other programming
languages.  However, as Python is so satisfying in many other areas, we
had some vague hope that he might become better in this particular one.
Especially given that, for a tiny moment, it once worked correctly for
French -- but sadly, this was the consequence of a Python bug which has
been corrected since.

François Pinard

More information about the Python-list mailing list