Naming Convention
Python Naming Conventions
Proper naming conventions in Python are crucial for maintaining readable and maintainable code. Following these conventions helps ensure consistency and understanding across a codebase, making it easier for developers to collaborate. Below are the general naming conventions in Python, adhering to PEP 8, the style guide for Python code.
General Naming Rules
Descriptive Names
Variables: Use meaningful and descriptive names that convey the purpose of the variable.
Functions and Methods: Name functions and methods using verbs that describe what they do.
Classes: Use nouns or noun phrases that describe what the class represents.
Case Styles
snake_case: Lowercase letters with underscores between words. Used for functions, variables, and methods.
PascalCase: Each word starts with a capital letter, with no underscores. Used for class names.
UPPER_SNAKE_CASE: Uppercase letters with underscores between words. Used for constants.
Specific Conventions
Variables
Use snake_case for variable names.
# Good
user_name = "John"
user_age = 30
# Bad
UserName = "John"
userAge = 30
Functions and Methods
Use snake_case for function and method names.
Function names should be descriptive and typically begin with a verb.
# Good
def get_user_name():
return "John"
def calculate_age(birth_year):
current_year = 2024
return current_year - birth_year
# Bad
def GetUserName():
return "John"
def calcAge(birthYear):
current_year = 2024
return current_year - birthYear
Classes
Use PascalCase for class names.
# Good
class User:
def __init__(self, name, age):
self.name = name
self.age = age
class UserManager:
def add_user(self, user):
pass
# Bad
class user:
def __init__(self, name, age):
self.name = name
self.age = age
class user_manager:
def add_user(self, user):
pass
Constants
Use UPPER_SNAKE_CASE for constant values.
# Good
MAX_USERS = 100
PI = 3.14159
# Bad
max_users = 100
Pi = 3.14159
Modules and Packages
Use snake_case for module and package names. Avoid using dashes
-
and spaces.
# Good
import user_manager
from utils import calculate_age
# Bad
import userManager
from Utils import calculate_age
Naming Conventions for Special Variables
Dunder (Double Underscore) Variables: Variables that start and end with double underscores are reserved for special use in the language. For example,
__init__
for object constructors,__name__
, and__main__
.
class User:
def __init__(self, name, age):
self.name = name
self.age = age
Naming Conventions in Specific Contexts
Instance and Class Variables
Use snake_case for instance and class variables.
Private variables should start with a single underscore
_
.
class User:
def __init__(self, name, age):
self._name = name # private variable
self.age = age
def get_name(self):
return self._name
Global Variables
Use UPPER_SNAKE_CASE for global variables to indicate that they are constants.
MAX_CONNECTIONS = 10
def connect():
pass
Method Arguments
Use snake_case for method argument names.
class User:
def __init__(self, user_name, user_age):
self.name = user_name
self.age = user_age
Exceptions
Use PascalCase for exception class names.
class CustomError(Exception):
pass
class UserNotFoundError(CustomError):
pass
Summary
Following naming conventions is essential for writing clean and maintainable Python code. These conventions include using snake_case
for variables and function names, PascalCase
for class names, and UPPER_SNAKE_CASE
for constants. Additionally, adhering to these conventions helps improve code readability and collaboration among developers.
Last updated