The best answers to the question “What is the naming convention in Python for variable and function names?” in the category Dev.
Coming from a C# background the naming convention for variables and method names are usually either camelCase or PascalCase:
// 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?
The Google Python Style Guide has the following convention:
A similar naming scheme should be applied to a
See Python PEP 8: Function and Variable Names:
Function names should be lowercase, with words separated by underscores as necessary to improve readability.
Variable names follow the same convention as function names.
mixedCase is allowed only in contexts where that’s already the prevailing style (e.g. threading.py), to retain backwards compatibility.
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.
David Goodger (in “Code Like a Pythonista” here) describes the PEP 8 recommendations as follows:
joined_lowerfor functions, methods,
camelCaseonly to conform to