See Python PEP 8: Function and Variable Names:
Answer from S.Lott on Stack OverflowFunction 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.
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.
The Google Python Style Guide has the following convention:
module_name,package_name,ClassName,method_name,ExceptionName,function_name,GLOBAL_CONSTANT_NAME,global_var_name,instance_var_name,function_parameter_name,local_var_name.
A similar naming scheme should be applied to a CLASS_CONSTANT_NAME
Hello!
How do you name your variables? Is there a standard?
I used to name my variables something like "firstNumber = 5" and constants like "MAX_SPEED = 200".
The problem I noticed is that when I look for those variables they are hard to spot on sight.
As such, I decided to write my next programs using notations like "number_of_items = 25".
I feel like using "_" is much more readable that using "numberOfItems = 25" that can look similar to "typesOfItems = 10"
-
We have to agree that variables names are an essential part of the source code and meaningful names are important when it comes to program comprehension.
-
They serve as an implied documentation that should convey to the reader;
-
Considering that sometimes names are the only documentation, then clean code approach should be highly regarded.
-
Therefore, it is important to devote a considerable amount of time to the issue of variable naming and therefore the variable names and rules.
Variable Names Rules & Guidelines
-
A variable name CAN be one word, though not a Must
-
You SHOULD separate words with underscores and not spaces.
-
When naming variable's we SHOULD only use alpha-numeric characters (letters and numbers) and the underscore character.
-
You SHOULD NOT start a variable name with a number, but can start with a letter or an underscore character. Breaking this rule will give you an invalid decimal literal or syntax error
-
A variable name SHOULD start with a letter or the underscore character.
-
You SHOULD NOT use python keywords and function names as variable names.
-
Variable names are case sensitive message, Message, MESSAGE and MeSsAge are different
-
Ensure that variable names are; Short: though they can be long. Descriptive: That is can tell what the variable is used for.
-
Take note that some letters like lowercase l and uppercase letter O can easily be confused with number 1 and 0.
-
While it is legal to use upper case letters; Others think it is a good idea to start variable names with lower case letter. Others believe that they better use camel case than separate names with_ . Others avoid starting variables names with an underscore, unless when they are writing library code.
References and further reading.
Watch this for code examples