470,647 Members | 1,112 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

style guideline for naming variables?

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 module
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 older
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 underscores
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 compatibility.

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

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 insistent
user could still gain access by calling Foo._Foo__a.) Generally, double
leading underscores should be used only to avoid name conflicts with
attributes in classes designed to be subclassed.

Note: there is some controversy about the use of __names (see below).


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:

some_function

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?
Mar 17 '06 #1
2 1668
John Salerno wrote:
some_function

But this seems awkward to me. someFunction seems nicer


Sorry, I was asking about variables but used a function example. But
really I'm asking about any situation where you might want to use
multiple words (except for classes, which is recommended to use Camel
case, and that seems fine).
Mar 17 '06 #2
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
module
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
older
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
underscores
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
compatibility.


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

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
insistent
user could still gain access by calling Foo._Foo__a.)
Generally, double
leading underscores should be used only to avoid name conflicts
with
attributes in classes designed to be subclassed.

Note: there is some controversy about the use of __names (see
below).

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:

some_function

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?


SomeClass

someFunction

some_variable

FWIW

Duncan
Mar 18 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by beliavsky | last post: by
31 posts views Thread by bart | last post: by
7 posts views Thread by Cameron Hatfield | last post: by
16 posts views Thread by E. Robert Tisdale | last post: by
4 posts views Thread by Mark Broadbent | last post: by
39 posts views Thread by Patrick | last post: by
93 posts views Thread by Phlip | last post: by
1 post views Thread by SpaceMarine | last post: by
1 post views Thread by Korara | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.