style guideline for naming variables?
buzzard at urubu.freeserve.co.uk
Sat Mar 18 02:18:33 CET 2006
John Salerno wrote:
> After reading the PEP, I'm still not quite sure if there is a
> recommended (or widely preferred) method of naming variables. Here are
> the relevant bits:
>> Global Variable Names
>> (Let's hope that these variables are meant for use inside one
>> only.) The conventions are about the same as those for functions.
>> Modules that are designed for use via "from M import *" should
>> use the
>> __all__ mechanism to prevent exporting globals, or use the the
>> convention of prefixing such globals with an underscore (which
>> you might
>> want to do to indicate these globals are "module non-public").
>> Function Names
>> Function names should be lowercase, with words separated by
>> as necessary to improve readability.
>> mixedCase is allowed only in contexts where that's already the
>> prevailing style (e.g. threading.py), to retain backwards
>> Method Names and Instance Variables
>> Use the function naming rules: lowercase with words separated by
>> underscores as necessary to improve readability.
>> Use one leading underscore only for non-public methods and instance
>> To avoid name clashes with subclasses, use two leading
>> underscores to
>> invoke Python's name mangling rules.
>> Python mangles these names with the class name: if class Foo has an
>> attribute named __a, it cannot be accessed by Foo.__a. (An
>> user could still gain access by calling Foo._Foo__a.)
>> Generally, double
>> leading underscores should be used only to avoid name conflicts
>> attributes in classes designed to be subclassed.
>> Note: there is some controversy about the use of __names (see
> It refers to instance variables, which I assume includes all variables
> that aren't global, and the suggestion is to follow function
> conventions, which would be this:
> But this seems awkward to me. someFunction seems nicer, but it is
> specifically mentioned not to do this for new code.
> So I'm just curious how other people handle the multiword situation.
> Underscores, or Pascal case?
More information about the Python-list