Skip to content

Python Function Naming Conventions and Best Practices

Updated: at 04:45 AM

Function naming is an important part of Python programming. Choosing clear, descriptive names for functions makes code more readable and maintainable. This guide will provide conventions and best practices for naming functions in Python based on the language’s official style guide PEP 8 and industry standards.

Table of Contents

Open Table of Contents

Introduction

Well-named functions are critical for writing understandable and maintainable Python code. The function name should clearly indicate what the function does without needing to look at the function body itself. This allows other developers to quickly comprehend the code’s purpose and functionality.

Function names also impact code organization. Following naming conventions organizes functions into logical groups and avoids name collisions between similarly named functions. Consistent naming makes it easier to find, use, and edit existing functions.

This guide will cover:

Following Python’s widely adopted naming conventions improves collaboration and enables others to understand your code faster.

PEP 8 Naming Conventions

PEP 8 is Python’s official style guide. It provides the following conventions for naming functions in Python:

These conventions aim to create easily readable function names that clearly express what each function does.

Best Practices

In addition to PEP 8’s conventions, several best practices have emerged for effectively naming functions:

Use Nouns, Verbs, and Adjectives

Mixing nouns, verbs, and adjectives together builds descriptive names like calculate_total_price().

Format Names with Underscores

Capitalize Acronyms and Constants

Avoid Vague Names

Do not use:

Handle Getters and Setters

Prefix getters and setters with get_ and set_ like:

get_name()
set_name(name)

This indicates their special behavior as accessor and mutator functions.

Name Private Functions with Underscores

Prefix private internal functions only used inside a class or module with a single underscore _ like:

def _internal_calc():
  # private internal calculation

This marks them as private APIs that should not be used externally.

Example Function Names

Here are examples of properly named functions based on the best practices:

Good

# Verb indicating action
def calculate_average(numbers):
  pass

# Noun indicating value returned
def name():
  return "John"

# Adjective adding specificity
def total_price(price, tax):
  return price + (price * tax)

# Verb + noun
def save_user(user):
  pass

# Verb + adjective + noun
def print_total_price(price, tax):
  pass

# Noun + noun
def car_name():
  pass

# Getter prefixed with get_
def get_username():
  pass

# Setter prefixed with set_
def set_username(name):
  pass

# Private function prefixed with _
def _private_method():
  pass

Bad

# Unclear, too short
def prc():
  pass

# Ambiguous abbreviation
def val(value):
  pass

# Vague, could mean multiple things
def process():
  pass

# Not separated by underscores
def CalculateTax():
  pass

# Unnecessary underscore
def get_user_name():
  pass

Conclusion

Properly naming functions is an important Python coding practice. PEP 8’s conventions of lowercase with underscores and descriptive verbs provide a standard for readable names.

Additional best practices like using nouns, adjectives, capitalizing acronyms, and avoiding vagueness further improve code clarity. Formatting getters, setters and private functions with prefixes also adds helpful context.

Adopting these naming conventions makes code more organized, maintainable and easier to understand for other developers. The practices enable collaborating and building upon existing code much faster in Python.