What is the naming convention in Python for variables and functions?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Python, the naming convention for variables and functions plays a crucial role in ensuring that the code is readable, maintainable, and intuitive for others (and for oneself) to understand. Following established conventions can also help in avoiding certain classes of bugs. Here’s a detailed look at these conventions, complete with examples and a summary table.
Understanding Naming Conventions
Naming conventions refer to rules guiding the choice of names for variables and functions in programming. In Python, these conventions are not just stylistic but are part of the Python Enhancement Proposal (PEP) guidelines which include PEP 8 -- Style Guide for Python Code. PEP 8 provides the foundation for how to structure Python code effectively.
Variables in Python
Variable names should be descriptive, excluding obfuscation or the use of irrelevant terms. Here are the main conventions:
- Lowercase with words separated by underscores: This form is also known as snake_case. For example,
user_ageorinventory_listare informative and adhere to the convention. - Avoid using letters 'l' (lowercase letter el), 'O' (uppercase letter oh), or 'I' (uppercase letter eye) as single character variable names as they can be confused with the numbers '1' and '0'.
Functions in Python
Function naming follows similar rules as variable naming:
- Use lowercase words separated by underscores: This maintains consistency and readability. For instance,
calculate_total()orsave_profile()clearly describe what the function does. - Use descriptive names: A function’s name should clearly indicate what the function does, and using a verb-object pair can be effective, e.g.,
send_message()orfind_element().
Examples
Here's a Python class example to demonstrate the use of these conventions:
Table: Summary of Naming Conventions
| Type | Convention | Example |
| Variable | snake_case | student_grade |
| Function | snake_case | calculate_average |
| Constant | UPPERCASE | MAX_LIMIT |
| Class | CapitalCamelCase | RecordFinder |
Additional Points
Constants
- Constants are typically defined at the top of a module and use all uppercase letters with words separated by underscores, e.g.,
MAX_OVERFLOW.
Classes
- Class names should normally use the CapWords convention where each word starts with a capital letter and there are no underscores:
BankAccount.
Underscore Prefix
- Single leading underscore
_var: conventionally used for "internal use” or “protected” objects. - Double leading underscore
__var: causing name mangling which affects the way a variable or method can be accessed, commonly used in class contexts to avoid name collisions of attributes with those in subclasses.
Acronyms
- When using acronyms, capitalization might vary, but usually if an acronym stands alone, all its letters should be uppercase, e.g.,
class HTTPResponse.
By adhering to these Python naming conventions, developers ensure that their code is consistent with the vast majority of the Python community. This makes your code more approachable and easier to integrate with other projects or libraries, ultimately improving readability and maintainability.

