Coming from a C# background the naming convention for variables and method names are usually either camelCase or PascalCase:
I personally use Java's naming conventions when developing in other programming languages as it is consistent and easy to follow. That way I am not continuously struggling over what conventions to use which shouldn't be the hardest part of my project!
Most python people prefer underscores, but even I am using python since more than 5 years right now, I still do not like them. They just look ugly to me, but maybe that's all the Java in my head.
I simply like CamelCase better since it fits better with the way classes are named, It feels more logical to have SomeClass.doSomething()
than SomeClass.do_something()
. If you look around in the global module index in python, you will find both, which is due to the fact that it's a collection of libraries from various sources that grew overtime and not something that was developed by one company like Sun with strict coding rules. I would say the bottom line is: Use whatever you like better, it's just a question of personal taste.
As mentioned, PEP 8 says to use lower_case_with_underscores
for variables, methods and functions.
I prefer using lower_case_with_underscores
for variables and mixedCase
for methods and functions makes the code more explicit and readable. Thus following the Zen of Python's "explicit is better than implicit" and "Readability counts"
David Goodger (in "Code Like a Pythonista" here) describes the PEP 8 recommendations as follows:
joined_lower
for functions, methods,
attributes, variables
joined_lower
or ALL_CAPS
for
constants
StudlyCaps
for classes
camelCase
only to conform to
pre-existing conventions
As the Style Guide for Python Code admits,
The naming conventions of Python's library are a bit of a mess, so we'll never get this completely consistent
Note that this refers just to Python's standard library. If they can't get that consistent, then there hardly is much hope of having a generally-adhered-to convention for all Python code, is there?
From that, and the discussion here, I would deduce that it's not a horrible sin if one keeps using e.g. Java's or C#'s (clear and well-established) naming conventions for variables and functions when crossing over to Python. Keeping in mind, of course, that it is best to abide with whatever the prevailing style for a codebase / project / team happens to be. As the Python Style Guide points out, internal consistency matters most.
Feel free to dismiss me as a heretic. :-) Like the OP, I'm not a "Pythonista", not yet anyway.
further to what @JohnTESlade has answered. Google's python style guide has some pretty neat recommendations,
Names to Avoid
\__double_leading_and_trailing_underscore__ names
(reserved by Python)Naming Convention
CapWords
for class names, but lower_with_under.py
for module names. Although there are many existing modules named CapWords.py
, this is now discouraged because it's confusing when the module happens to be named after a class. ("wait -- did I write import StringIO
or from StringIO import StringIO
?")Guidelines derived from Guido's Recommendations