Close

Python - Naming Conventions

[Last Updated: Feb 5, 2022]

Functions and variable names

In Python function and variable names should be lowercase and words should be separated by underscores.

Examples:

prime_number = 17
def run_process():
       pass

Class names

Class names should normally use the camel case. For different case styles see here.

Example

class MyClass:
      pass

Instance variables and method names

Just like function names, the method and instance variable names should be all lowercase and words should be separated by underscores.
Non-public methods and instance variables should begin with a single underscore

Example

class MyClass:
    def __init__(self):  # constructor
        self.__counter = 1

    def _increase_counter(self):
        ++self.__counter
        print(self.__counter)

    def work(self):
        self._increase_counter()

my_class = MyClass()
my_class.work()

Method arguments

The first argument to instance methods should be self.
The first argument of the class methods should be cls.

Example

class Log:
    @classmethod
    def info(cls, arg):
        cls._print_info(arg)

    @classmethod
    def _print_info(cls, arg):
        print("info: " + arg)


Log.info("test")

See Also