Python naming conventions are guidelines that help ensure consistency and readability in Python code. They are part of PEP 8, the Python Enhancement Proposal that outlines coding standards for Python. Here are the key conventions:
Naming Conventions for Different Elements
- Variables and Functions:
- Use lowercase letters and separate words with underscores (
snake_case
). - Example:
my_variable
,calculate_area()
-
# Correct number_of_students = 30 def calculate_area(width, height): return width * height # Incorrect NumberOfStudents = 30 def CalculateArea(width, height): return width * height
- Use lowercase letters and separate words with underscores (
- Classes:
- Use CapitalizedWords convention (
CamelCase
). - Example:
MyClass
,Person
-
# Correct class Student: def __init__(self, name, age): self.name = name self.age = age # Incorrect class student: def __init__(self, name, age): self.name = name self.age = age
- Use CapitalizedWords convention (
- Constants:
- Use all uppercase letters with underscores separating words.
- Example:
PI
,MAX_CONNECTIONS
-
# Correct PI = 3.14159 MAX_CONNECTIONS = 100 # Incorrect Pi = 3.14159 MaxConnections = 100
- Modules and Packages:
- Use short, all-lowercase names. Underscores can be used if it improves readability.
- Example:
module_name
,package_name
-
# Correct # Filename: my_module.py def my_function(): pass # Incorrect # Filename: MyModule.py def MyFunction(): pass
- Global Variable Names:
- Use a single leading underscore to indicate non-public variables.
- Example:
_private_variable
-
# Correct _global_variable = "I am a global variable" # Incorrect GlobalVariable = "I am a global variable"
- Instance Variables:
- Follow the same convention as variables and functions (
snake_case
). - Example:
self.my_variable
-
# Correct class Car: def __init__(self, make, model): self.make = make self.model = model # Incorrect class Car: def __init__(self, Make, Model): self.Make = Make self.Model = Model
- Follow the same convention as variables and functions (
- Method Names:
- Use lowercase with words separated by underscores.
- Example:
my_method()
-
# Correct class Animal: def make_sound(self): print("Sound") # Incorrect class Animal: def MakeSound(self): print("Sound")
- Private Methods and Variables:
- Use a single leading underscore to indicate non-public methods and variables.
- Example:
_my_private_method()
,_my_private_variable
-
# Correct class Example: def __init__(self): self._private_variable = "I am private" def _private_method(self): pass # Incorrect class Example: def __init__(self): self.privateVariable = "I am private" def privateMethod(self): pass
- Special Methods:
- These are also known as “magic methods” and are surrounded by double underscores.
- Example:
__init__()
,__str__()
-
# Correct class CustomObject: def __init__(self, value): self.value = value def __str__(self): return f"CustomObject with value {self.value}" # Incorrect class CustomObject: def __init__(self, value): self.value = value def Str(self): return f"CustomObject with value {self.value}"
Additional Guidelines
- Avoid Using Built-in Names:
- Avoid using names that are the same as Python’s built-in functions or modules.
- Example: Avoid naming a variable
list
,str
, etc.
- Meaningful Names:
- Choose names that are descriptive of the purpose or usage of the variable, function, or class.
- Example: Instead of
x
, usewidth
orheight
.
- Function and Method Arguments:
- When a function or method accepts more than one argument, consider using positional or keyword arguments for clarity.
- Example:
def calculate_area(width, height):
- Indentation and Spacing:
- Use 4 spaces per indentation level.
- Ensure proper spacing around operators and after commas.
Following these naming conventions helps maintain a codebase that is clean, readable, and maintainable. It also makes it easier for others to understand and contribute to your code.