views:

3010

answers:

10

Coming from a C# background the naming convention for variables and method names are usually either CamelCase or Pascal Case:

// C# example
string thisIsMyVariable = "a"
public void ThisIsMyMethod()

In Python, I have seen the above but I have also seen underscores being used:

# python example
this_is_my_variable = 'a'
def this_is_my_function():

Is there a more preferable, definitive coding style for Python?

+30  A: 

See Python PEP 8.

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

Variables...

Use the function naming rules: lowercase with words separated by underscores as necessary to improve readability.

Personally, I deviate from this because I also prefer mixedCase over lower_case for my own projects.

S.Lott
upvoted for an informative answer but then downvoted for preferring an ugly naming convention :-D
Dan
i to prefer camelCase personally I think that using_underscores is ugly.
Unkwntech
PEP = Python Enhancement Proposal.
Peter Mortensen
must_use_underscores!
morganchristiansson
+9  A: 

Behold the Python Style Guide.

Ben Hoffstein
+1 for a more dramatic answer :).
MAK
A: 

The coding style is usually part of an organization's internal policy/convention standards, but I think in general, the all-lower-case-underscore-separator style is most common in python.

fuentesjr
+4  A: 

There is PEP 8, as other answers show, but PEP 8 is only the styleguide for the standard library, and it's only taken as gospel therein. One of the most frequent deviations of PEP 8 for other pieces of code is the variable naming, specifically for methods. There is no single predominate style, although considering the volume of code that uses mixedCase, if one were to make a strict census one would probably end up with a version of PEP 8 with mixedCase. There is little other deviation from PEP 8 that is quite as common.

Thomas Wouters
+2  A: 

Most python guys 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.

André
A: 

Personally I try to use CamelCase for classes, mixedCase methods and functions. Variables are usually underscore separated (when I can remember). This way I can tell at a glance what exactly I'm calling, rather than everything looking the same.

crystalattice
Camel case starts with a lowercase letter IIRC like "camelCase".
Unkwntech
+10  A: 

David Goodger (in "Code Like a Pythonista" here) describes the PEP 8 recommendations as follows:

  • joined_lower for functions, methods, attributes

  • joined_lower or ALL_CAPS for constants

  • StudlyCaps for classes

  • camelCase only to conform to pre-existing conventions

bvmou
+1  A: 

Typically, one follow the conventions used in the language's standard library.

Justice
+5  A: 

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"

claytron
+1 I switch those two (I use mixedCase for variables), but having everything more distinct like that helps make it immediately obvious what you're dealing with, especially since you can pass around functions.
Xiong Chiamiov
+1  A: 

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

Jonik