Looking for Python naming conventions - variables, classes? When to use underscore and when PascalCase? If so this article will show examples of most used ones plus resources for further reading.
Naming conventions must be followed by Python programmers for readability and clearness of the code. Java uses Snake case as a practice for writing names of methods and variables.
Python naming convention by examples
Below you can find a list of names for Classes, Variables, Underscore, Files, Modules, Global and Private variables etc - which are taken from Google Python Style Guide. These examples are perfect for beginners and new to Python:
As you can see the most frequent is the Snake Case for Python naming.
Table for Python naming convention
Great practical table for Python naming convention is published here: naming-convention-guides/python
Python naming convention for variables - variable_name
The one of the most important and most violated conventions is for variables in any programming language. I can see many places where the convention is abused which cause confusions and conflicts.
The rules for variable naming in Python are simple:
- lowercase only
- don't start with special characters -
- separator is
- avoid single character variables like
n( try to use descriptive names for your methods and variables)
- private variables should start with underscore
class Person: age = None # public variable last_name = None # public variable first_name = None # public variable _id = 0 # Private variable
Note: one character variable is fine for loops -
for x in range(1, 11):
Python naming rules for classes - ClassName
Python convention for classes uses Pascal Case (don't confuse it with Camel case where the first letter is lowercase) - words are written together and all starts with capital letter - PascalCase.
The rules for naming of classes in Python are simple too:
- words start uppercase letters
- nouns e.g. Person, Animal, PersonAddress
class PersonAddress: ...
private class should start with underscore -
class _PersonInfo: # private class ...
Note: Camel Case vs Pascal Case
- Camel Case (ex: someVar, someClass, somePackage.xyz).
- Pascal Case (ex: SomeVar, SomeClass, SomePackage.xy
Python naming rules for functions
The naming rules for functions in Python are similar to variable names:
Function names should be lowercase, with words separated by underscores as necessary to improve readability.
It's interesting to mention Function and Method Arguments rules:
Always use self for the first argument to instance methods.
Always use cls for the first argument to class methods.
If a function argument's name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus class_ is better than clss. (Perhaps better is to avoid such clashes by using a synonym.)
Python naming convention for files and directories
For files and directories naming convention can be read in PEP 8:
- files -
- Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.
- directories -
- Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.
Note: For Python packages -
although the use of underscores is discouraged is preferable but not forbidden.