Python Programming Naming Style Cheatsheet

Category Case Example
Modulessnake_caseimport my_module
Packagessnake_casefrom my_package import ...
Variablessnake_casemy_variable = 10
ConstantsSCREAMING_SNAKE_CASEMY_CONSTANT = 10
Functionssnake_casedef my_function(): ...
Methodssnake_casedef my_method(self): ...
ClassesPascalCaseclass MyClass: ...
ExceptionsPascalCaseclass MyException(Exception): ...
Propertiessnake_case@property def my_property(self): ...
Global Variablessnake_caseglobal my_global
Private Variables_snake_case_my_variable = 10
Private Methods_snake_casedef _my_method(self): ...

Additional Notes

Type Variables and Covariant/Contravariant Behavior

When using type variables in Python (introduced in PEP 484), it's recommended to use CapWords for type variable names. Common examples include short names like T, AnyStr, or Num.

Additionally, when declaring covariant or contravariant behavior in type variables, you can use the suffixes _co for covariant types and _contra for contravariant types. Here's an example:

from typing import TypeVar

VT_co = TypeVar('VT_co', covariant=True)
KT_contra = TypeVar('KT_contra', contravariant=True)
Logo
buildsoftwaresystems.com