HOWTO · Python

Naming Convention for Functions, Classes, Constants, and Variables in Python

Learn Python naming conventions for functions, classes, constants, and variables, with clear examples based on PEP 8.

On this page

Python naming conventions make code easier to read and maintain. PEP 8 is Python’s style guide; its conventions are guidance, not rules enforced by the language. Use them as a consistent starting point, while following a project’s established style when it differs. See the PEP 8 naming conventions for the source guidance.

Naming Conventions for Functions

Use lowercase words separated by underscores for function names. This style is called snake_case; a name such as calculate_area describes the action and is easy to scan. Choose names that tell readers what the function does.

For example, this function calculates and displays the area of a rectangle:

def calculate_area(length, width):
    return length * width

area = calculate_area(5, 3)
print(area)

Output:

15

The function name uses a verb, and its parameters describe the values it accepts. The call passes a length of 5 and width of 3, so the returned area is 15.

Python identifiers are case-sensitive, so calculate_area and Calculate_Area are different names. A name can contain letters, digits, and underscores, but it cannot begin with a digit. Keep names readable instead of encoding extra details that the function body or surrounding code already makes clear. A single leading underscore, as in _parse_record, signals that a name is intended for internal use; it does not make the name inaccessible or private. Follow the surrounding project’s conventions when they differ.

Naming Conventions for Classes

Use CapWords for class names: capitalize the first letter of each word and do not separate words with underscores. This style is also commonly called PascalCase. It distinguishes a class such as Car from a function or variable name.

class Car:
    def __init__(self, make, model):
        self.make = make
        self.model = model

my_car = Car("Toyota", "Corolla")
print(my_car.make, my_car.model)

Output:

Toyota Corolla

Here, Car names the class, while my_car follows the lowercase underscore-separated style used for variables. The initializer stores the supplied make and model on the new object.

Naming Conventions for Constants

Write constants as uppercase words separated by underscores, usually at module level. For example, MAX_RETRIES signals a value intended to remain stable. Uppercase spelling is a convention only: Python does not make a value immutable because its name is uppercase, so avoid reassigning it by agreement and practice.

PI = 3.14159

def calculate_circle_area(radius):
    return PI * (radius ** 2)

area = calculate_circle_area(5)
print(area)

Output:

78.53975

The constant name makes its intended role visible, and the function uses it to calculate the circle’s area.

Naming Conventions for Variables

Use lowercase words separated by underscores for variables, as with functions. Prefer a descriptive name such as user_age over a vague name such as x; the right amount of detail depends on how much context the surrounding code provides.

user_age = 30
def display_age(age):
    print(f"The user is {age} years old.")

display_age(user_age)

Output:

The user is 30 years old.

user_age describes the stored value, and age is clear as a parameter in the short function. Consistent names help readers follow how values move through a program. Python does not enforce these naming styles, so apply the conventions consistently and adapt to the style used by your project.