*Naming Conventions*

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Mon Jun 4 22:51:20 CEST 2007

On Jun 4, 12:20 am, Ninereeds <stephenhorne... at aol.com> wrote:
> On Jun 4, 5:03 am, Thorsten Kampe <thors... at thorstenkampe.de> wrote:
> > for validanswer in validanswers:
> >     if myAnswers.myanswer in myAnswers.validAnswers[validanswer]:
> >         MyOptions['style'] = validanswer
> First, for small loops with loop variables whose meaning is obvious
> from context, the most readable name is usually something like 'i' or
> 'j'.

'i' and 'j' are the canonical names for for loops indices in languages
that don't support proper iteration over a sequence. Using them for
the iteration variable of a Python for loop (which is really a
'foreach' loop) would be at best confusing.

> Based on this, your code might become something like...
>   for i in validanswers:
>     if myAnswers.current in myAnswers.validList [i]:
>       MyOptions['style'] = i

And this is *exactly* what one should not do in Python. If you want a
generic name here, use 'item' instead.

> It is also often useful to use a convention where a prefix identifies
> whether a name is a (p)arameter, (l)ocal variable, or (m)ember

You don't have to use any kind of prefix for attributes ('member' is
not a Python idiom - remember that in Python, a method is also an
attribute...) since the use of 'self' is mandatoy.

And you usually don't need to distinguish 'local' from 'parameters',
1/ parameters names *are* local
2/ pythonic methods and functions tends to be short, so you usually
know which names are params.

FWIW, prefixing names this way is something I've almost never seen in
Python code.

> variable - no prefix for functions, usually. For example, a simple
> setter method

Python has a pretty good support for computed attributes (look for
'property'), so you don't need explicit getters/setters.

More information about the Python-list mailing list