Python File Naming Conventions

Python File Naming Conventions

All the naming conventions discussed in this article are based on the PEP 8 styling guide for Python code.

The Python library’s naming conventions give some room to choose from, so there isn’t a lot of consistency.

Python, a vast language, has two halves: the existing libraries and functions. While the other is the third-party frameworks and packages.

The former follows a consistency, whereas the naming might vary in the latter’s case.

The Naming Conventions in Python

General Do’s/Dont’s of Naming conventions in Python:

  • Do not use generic names that might be similar to predefined functions.
  • Do not use too wordy names as they increase the room for error while using the function multiple times.
  • The characters l, O, or I individually should not be used for naming. These characters are sometimes misinterpreted as 0 or 1 in some fonts.
  • When the CamelCase convention is utilized, the letters of all the abbreviations are capitalized.

Modules:

  • All modules should be named in only lowercase characters.
  • An underscore suits the gap when more than one word needs to be used for naming the module.
  • The use of a single word for naming the modules is recommended.

Packages:

  • Similar to modules, packages should be named only with the help of lowercase letters.
  • An underscore suits the gap when more than one word needs to be used for naming the module.
  • Using a single word for the naming of packages is recommended.

Classes:

  • The use of the CapWords convention is generally recommended for naming Classes.
  • However, the in-built classes in Python are usually in lowercase.
  • It is recommended that all the Exception classes have the word Error at the end of their names.

Global Variables:

  • These variables are recommended to use all lowercase letters.
  • If needed, the words can be detached by an underscore sign.

Variables:

  • Similar to global variables, instance variables should also use lowercase letters.
  • If needed, the words can be detached by an underscore sign.
  • In rare cases, the beginning of an instance variable could be with double underscores.

Functions/Methods:

  • Functions or methods should always utilize lowercase letters for their naming.
  • If needed, the words can be detached by an underscore sign.
  • A function beginning with a single underscore usually marks the naming of a non-public function.
  • In rare cases, a method’s beginning could be with double underscores.

Function Arguments:

  • The first argument of the Instance methods needs to be self.
  • The first argument of class methods needs to be cls.

Constants:

  • Constants use all Uppercase letters.
  • If there are multiple words, an underscore can separate them.