Python Naming Conventions — The 10 Points You Should Know
Coding standards are a set of guidelines to produce maintainable and scalable code. And how good are you at following them could decide the longevity of your career in Software Development. Since I code in Python, so I’ll be talking about the Python naming conventions in this post.
I’ve derived this information from Python’s PEP8 documentation and tried to put it in simple word. So I could make it easier for the readers to digest.
Also, do remember that naming convention is just one of the facets out of many Python coding standards. You can read about the other factors from the PEP8 documentation.
With this information, you can ensure code quality, readability, and re-usability. However, you can refer this Python code optimization guide to improve the performance and robustness of your Python applications.
Naming Conventions in Python
I. What to keep and what to avoid?
- How to start?
Don’t use names that look too general or wordy.
Maintain a fine line between the two.
- What is bad?
user_list, moveInts, swapNums, dict_to_store_defns_of_a_word
- What is good?
user_info, move_integers, swap_numbers, word_definitions.
- Things to note-
Don’t be naive and name things like “X,” “Y” or “Z.”
Use CamelCase only when it makes sense.
II. How to name a Package in python?
- Only use lower case while naming a Package in Python.
- Use an underscore as a separator to join multiple words when needed and produce a logical name.
- However, keeping a short name (preferably one word long) is what PEP 8 prefer.
III. What should be my Module name?
- Python loves the small case alphabets and hence suggests Module names should also follow the suite.
- Like Packages, combine multiple words with an underscore to generate a meaningful module name.
- Don’t think twice if you already have a perfect single word name for your module.
IV. How to choose a Classe name in Python?
- Classes in Python subject to a different naming scheme called as CapWords convention.
- In some cases, if the class is callable (allows you to use round parenthesis) and has proper documentation, then you can name them like functions.
- Contrary to the above principle, Python has its built-in classes with names in lowercase.
- Exception classes should always have trailing word as Error.
V. Should I name Globals differently?
- Global variables must all be in lowercase.
- The underscore should be used as a separator while forming a variable with multiple words.
VI. What is a good Instance variable name?
- Instance variable names should follow the lowercase convention.
- Have the underscore as a separator while naming a multi-word instance variable.
- Begin a non-public instance variable name with a single underscore.
Use two consecutive underscores at the beginning of an instance variable. It will get the name mangled (kind of like a private member).
VII. Don’t mess with a Method name in Python.
- Make only lowercase Method names.
- An underscore should separate words in a method name.
- The non-public methods should have a leading underscore.
- Mangling a method name will require adding two underscores at the beginning.
VIII. Never ignore the Arguments of a method.
- Every instance method should have ‘self’ as its first argument.
- All class methods should have ‘cls’ as their first argument.
IX. A function name should convey what it does.
- Function names should follow the lowercase naming convention.
- They should also use an underscore to separate words in a function name.
X. Ignorance is bliss but not in case of Constants in Python.
- You should be able to identify a Constant just by looking at its name.
- Hence, they all must be in a fully capitalized format.
- Words should separate by an underscore to form a long constant name.