Python Naming Convention Examples for Classes, Variables, Underscore, Files

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:

  • 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

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

Type Public Internal
Packages lower_with_under
Modules lower_with_under _lower_with_under
Classes CapWords _CapWords
Exceptions CapWords
Functions lower_with_under() _lower_with_under()
Global/Class Constants CAPS_WITH_UNDER _CAPS_WITH_UNDER
Global/Class Variables lower_with_under _lower_with_under
Instance Variables lower_with_under _lower_with_under
Method Names lower_with_under() _lower_with_under()
Function/Method Parameters lower_with_under
Local Variables lower_with_under

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 _ - underscore
  • avoid single character variables like x, n ( try to use descriptive names for your methods and variables)
  • private variables should start with underscore

example:

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

example:

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 - module_name:
    • Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.
  • directories - package_name:
    • 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.

Resources

Share Tweet Send
0 Comments
Loading...