473,325 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 1794
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: beliavsky | last post by:
Is there a more recent set of Python style guidelines than PEP 8, "Style Guide for Python Code", by van Rossum and Warsaw, at http://www.python.org/peps/pep-0008.html , which is dated July 5, 2001?
31
by: bart | last post by:
Why is it that everybody likes the follwowing curly braces syntax function() { implementations; } I find the following much better to read (C style) function() {
7
by: Cameron Hatfield | last post by:
I was wondering how good this page of coding standards (http://www.doc.ic.ac.uk/lab/cplus/c++.rules/) is. Is it too outdated?
16
by: E. Robert Tisdale | last post by:
C++ Programming Style Guidelines http://geosoft.no/development/cppstyle.html I think that these guidelines are almost *all* wrong. For example: 11. Private class variables should have...
4
by: Mark Broadbent | last post by:
stupid question time again to most of you experts but this is something that continually bothers me. I am trying to get into the habit of naming variables and controls in an assembly as per...
39
by: Patrick | last post by:
The c# code style guide that I follow suggests that class variables (fields) be coded with camel casing, like this: int recordId; string name; It also suggests that variables within methods...
93
by: Phlip | last post by:
C++ers: Feast your eyes: void Home:: inherits (IdentifierPtr const& id) { ... }
16
ADezii
by: ADezii | last post by:
Indent - A procedure normally has several sections. There may be a section for error-handling code, groups of code lines for repetitive operations, alternative groups of statements to be executed...
1
by: SpaceMarine | last post by:
hello, i thought i remember seeing at one point a "recommended" style guide written for C#. this .NET/csharp guide was written by microsoft and covered things like case, formatting, naming...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.