Ray Vega Ray Vega - 6 months ago 12
Python Question

What is the naming convention in Python for variable and function names?

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?

Answer

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.

Comments