[Tutor] python_naming_conventions - newbie

Erik Price erikprice at mac.com
Sat Oct 18 11:17:26 EDT 2003


On Friday, October 17, 2003, at 01:51  AM, Rahul Kumar wrote:

> As someone learning python after several years of working on Java, i am
> interested to know why does python follow the
> lower_case_with_underscores way of naming methods and variables while
> Java follows the MixedCaseWithoutUnderscores.

I don't know the answer why, but one of the more important suggestions 
made by both "Java Elements of Style" and IIRC the Python Style 
Guidelines is that convention is more important than aesthetics.  That 
is to say, while aesthetics certainly play a role (even in the very 
choice of a name for a variable), what is more important is to make 
sure that your naming is consistent with what is conventional for your 
project.  Java happens to have a well-known published style guide that 
strongly recommends that camelCase be used for variable names and 
UNDERSCORE_SEPARATORS only be used in [capitalized] constant 
declarations (static final String FOO).

That said, my guess is that the reason why it was originally selected 
is probably because the extra underscores make the variable names 
wider.  This isn't as much an issue in Python because type and access 
declarations aren't used, but because you need to declare the access 
level and type/return type in Java, it ends up making your code lines 
wider.  Just yesterday I wrote this method signature:

     private Mip mipFromFile(File mipFile)

whereas in Python, it could have simply been declared as:

     def mip_from_file(mip_file):

As you can see, the Python version is still shorter even though I've 
used the underscores as separators.

> After writing a couple of py progs, i find myself using the python way
> in Java, and i prefer it (used to write C progs this way 10 yrs ago), 
> and
> i need some justification for this, since i am the guy who made the 
> Java
> guidelines for my org 4 years back !

In Python I don't really stick to one or the other, but try to go with 
what seems most logical for the situation.  However, in Java my own 
preference is to simply stick to the official standard simply because 
when others read my code, that's probably what they'll expect to see.


Erik




More information about the Tutor mailing list