Common Python naming conventions and their specific uses.
|
Use in Python
|
Preferred Convention
|
Example
|
|
Variables
|
snake_case
|
total_cost
|
|
Functions and methods
|
snake_case
|
calculate_total()
|
|
Modules and packages
|
short snake_case
|
grade_tools.py
|
|
Classes
|
PascalCase
|
GradeCalculator
|
|
Exceptions
|
PascalCase
|
InvalidGradeError
|
|
Constants
|
UPPER_SNAKE_CASE
|
MAX_ATTEMPTS = 3
|
|
Convention
|
Example
|
Where you may see it
|
Python status
|
|
lowercase
|
score
|
Simple names
|
Common
|
|
snake_case
|
student_score
|
Python variables, functions, methods, modules
|
PEP 8 preferred
|
|
PascalCase (CapitalCase)
|
StudentScore
|
Classes and exceptions
|
PEP 8 preferred
|
|
camelCase
|
studentScore
|
JavaScript, Java, C#
|
Avoid for Python names
|
|
UPPER_SNAKE_CASE (SCREAMING_SNAKE_CASE)
|
MAX_SCORE
|
Constants and configuration
|
PEP 8 preferred
|
|
kebab-case (train-case)
|
student-score
|
URLs, CSS classes, command-line options
|
Not valid for Python identifiers
|
|
dot.case
|
student.score
|
Module paths and attribute access
|
Not a single identifier
|
|
Title Case
|
Student Score
|
Headings and human-facing labels
|
Not a Python identifier
|
|
flatcase
|
studentscore
|
Occasional file names or labels
|
Rare; less readable
|
|
UPPERCASE
|
MAXSCORE
|
Acronyms or legacy systems
|
Rare; avoid for new Python names
|
|
Pattern
|
Example
|
Meaning
|
|
Leading underscore
|
_cached_result
|
Conventionally internal or non-public. from module import * does not import it.
|
|
Trailing underscore
|
class_
|
Avoids a conflict with a Python keyword or built-in name.
|
|
Double leading underscore
|
__balance
|
Python applies name mangling inside a class to help avoid accidental subclass conflicts.
|
|
Double leading and trailing underscores (dunder)
|
__init__, __len__
|
Special names defined by Python. Do not invent new dunder names.
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.