Python
Naming Convention
Programming
Variables
Functions

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_age or inventory_list are 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() or save_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() or find_element().

Examples

Here's a Python class example to demonstrate the use of these conventions:

python
1class DatabaseConnector:
2    def __init__(self, server_address):
3        self.server_address = server_address
4
5    def connect_to_server(self):
6        # Implementation code here
7        pass
8
9    def retrieve_data(self, query):
10        # Implementation code here
11        pass
12
13# Example of variable naming
14user_profile = {
15    'user_name': 'john_doe',
16    'user_age': 30
17}
18
19# Function usage
20def fetch_user_profile(user_id):
21    # Implementation code here
22    pass

Table: Summary of Naming Conventions

TypeConventionExample
Variablesnake_casestudent_grade
Functionsnake_casecalculate_average
ConstantUPPERCASEMAX_LIMIT
ClassCapitalCamelCaseRecordFinder

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.


Course illustration
Course illustration

All Rights Reserved.