patternpythonCritical
What is the naming convention in Python for variables and functions?
Viewed 0 times
functionsnamingpythontheandforwhatvariablesconvention
Problem
Coming from a C# background the naming convention for variables and methods are usually either camelCase or PascalCase:
In Python, I have seen the above but I have also seen snake_case being used:
Is there a more preferable, definitive coding style for Python?
// C# example
string thisIsMyVariable = "a"
public void ThisIsMyMethod()
In Python, I have seen the above but I have also seen snake_case being used:
# python example
this_is_my_variable = 'a'
def this_is_my_function():
Is there a more preferable, definitive coding style for Python?
Solution
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.
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.
Context
Stack Overflow Q#159720, score: 1182
Revisions (0)
No revisions yet.