Tip 5 Keep Letter Case Consistent

Pythonic Programming — by Dmitry Zinoviev (13 / 116)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Use Quotes of All Sorts | TOC | Wrap Long Lines 👉

★2.7, 3.4+ Any Python identifier must start with a Latin letter (“a” through “z” or “A” through “Z”) or an underscore and contain only Latin letters, decimal digits (“0” through “9”), or underscores. It is advised that identifiers are short (to facilitate typing) and descriptive.

Additionally, use all uppercase letters for the so-called constants. Python does not have constants as such. You can change the value of any variable. However, when you spell an identifier in all capital letters, you suggest that the variable’s value should not be changed. Here is a constant:

​ PI = 3.14159 ​# But please use math.pi instead!​

Use identifiers in lowercase, connected with underscores, if necessary, or mixedCase identifiers for variables, functions, and modules. Here are variable, function, and module identifiers:

​ my_cat_s_age = 11
​ myCatSAge = 11
​ _var = ​'I do not care'​
​ ​def​ ​silly_function​():
​ ​pass​
​ ​def​ ​anotherSillyFunction​():
​ ​pass​
​ ​import​ ​myjunk​

Use CapitalizedWords for classes (but not for class objects). Here is a class identifier:

​ ​class​ ADoNothingClass:
​ ​pass​

Readability counts!

👈 Use Quotes of All Sorts | TOC | Wrap Long Lines 👉

Pythonic Programming by Dmitry Zinoviev can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.