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:
- Python’s official naming conventions from PEP 8
- Industry best practices for function names
- Use of verbs, nouns, and adjectives
- Formatting with underscores and capitalization
- Avoiding vague or generic names
- Handling getters and setters
- Naming private functions
- Example function names
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:
-
Lowercase with words separated by underscores - Function names should be all lowercase with underscores between words, like
do_something()
. -
Verbose rather than short - Prefer longer, more descriptive names over abbreviations.
-
Use verbs for function names - The name should describe what the function does, so using verbs like
calculate_TAX()
is recommended. -
Avoid ambiguity - Don’t use names that could have multiple meanings like
check()
or abbreviations likeval()
.
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
-
Verbs indicate an action like
calculate()
,render()
,fetch()
etc. This is the most common format. -
Nouns describe or return a value like
name()
,user()
. -
Adjectives add specificity like
total_price()
.
Mixing nouns, verbs, and adjectives together builds descriptive names like calculate_total_price()
.
Format Names with Underscores
-
Separate words with underscores rather than camelCase. Python reserves camelCase for class names.
-
You can also separate logical groups of words using underscores like
calculate_total_order_price()
. -
Avoid unnecessary underscores like
calculate_price__tax()
.
Capitalize Acronyms and Constants
- Capitalize acronyms and constants like
HTTP
orPI
ascalculate_HTTP_traffic()
ordraw_PI_chart()
.
Avoid Vague Names
Do not use:
-
Generic terms like
process()
,handle()
,manage()
,data()
. -
Names with multiple meanings like
check()
,file()
,object()
. -
Abbreviations like
val()
,calc()
unless they are widely known.
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.