Naming Standards (Snake Case vs Pascal Case Forms)
🏷️ Python Scripting Best Practices / Code Style and PEP 8
When writing Python scripts, the names you choose for variables, functions, and classes matter more than you might think. Consistent naming makes your code readable, maintainable, and predictable for other engineers who may work with your scripts later. Python follows specific naming conventions outlined in PEP 8, the official style guide. The two most common forms you will encounter are Snake Case and Pascal Case.
⚙️ What Are These Naming Forms?
Naming forms define how you combine multiple words into a single identifier. Python uses different forms depending on what you are naming.
- Snake Case: All lowercase letters with words separated by underscores. Example: load_config_file
- Pascal Case: Each word starts with a capital letter, with no separators. Example: LoadConfigFile
There is also Camel Case (like Pascal Case but with a lowercase first letter), but Python rarely uses it. Stick to Snake Case and Pascal Case for clean, Pythonic code.
📊 When to Use Each Form
Python has clear rules about which form applies to which type of identifier.
| Identifier Type | Naming Form | Example |
|---|---|---|
| Variable names | Snake Case | server_count |
| Function names | Snake Case | get_disk_usage |
| Method names | Snake Case | connect_to_database |
| Module (file) names | Snake Case | network_utils.py |
| Package names | Snake Case (no underscores preferred) | netutils |
| Class names | Pascal Case | DatabaseConnector |
| Exception names | Pascal Case | ConnectionTimeoutError |
| Constant names | Uppercase Snake Case | MAX_RETRY_COUNT |
🛠️ Snake Case in Practice
Snake Case is the most common naming form you will use as a Python engineer. It applies to almost everything except classes and exceptions.
Variables hold data values and should describe their content clearly: - active_connections instead of ac - error_message instead of errmsg - total_disk_space_gb instead of tdsg
Functions perform actions and should start with a verb: - parse_log_file instead of log_parser - validate_ip_address instead of ip_valid - send_alert_notification instead of alert
Module files should be short and descriptive: - file_operations.py instead of fileOps.py - monitoring_tools.py instead of monTools.py
🏗️ Pascal Case in Practice
Pascal Case is reserved for classes and exceptions. This distinction helps you immediately identify when you are working with an object-oriented construct.
Classes represent blueprints for objects: - ConfigManager instead of config_manager - NetworkScanner instead of network_scanner - AlertHandler instead of alert_handler
Exceptions are special classes that indicate error conditions: - FileNotFoundError instead of file_not_found_error - ConnectionRefusedError instead of connection_refused - InvalidConfigurationError instead of invalid_configuration
🕵️ Common Mistakes to Avoid
New Python engineers often mix up these forms. Watch out for these pitfalls.
Using Pascal Case for variables or functions: - ❌ ServerName = "web01" - ✅ server_name = "web01"
Using Snake Case for classes: - ❌ class database_connector: - ✅ class DatabaseConnector:
Using mixed styles within the same script: - ❌ getUserData and parse_log_file in the same file - ✅ Stick to one style per identifier type
Using single-letter names (except in very short loops): - ❌ x = get_data() - ✅ response_data = get_data()
🧩 Quick Reference Cheat Sheet
Keep this simple rule in mind:
- If you are naming a thing that stores data → use Snake Case
- If you are naming an action (function) → use Snake Case
- If you are naming a blueprint (class) → use Pascal Case
- If you are naming a file → use Snake Case
- If you are naming a constant value → use UPPER_SNAKE_CASE
✅ Final Thoughts
Consistent naming is one of the easiest ways to improve your Python code quality. By following Snake Case for variables, functions, and modules, and Pascal Case for classes and exceptions, you align with the broader Python community. Other engineers will thank you when they read your scripts, and you will thank yourself when you revisit code you wrote months ago.
Naming standards define how engineers write variable, function, and class names in Python to keep code consistent and readable.
✅ Example 1: Snake case for a simple variable
This example shows the standard way to name a variable using lowercase letters and underscores.
user_name = "Alice"
print(user_name)
📤 Output: Alice
✅ Example 2: Pascal case for a class name
This example shows how to name a class using capitalized words with no underscores.
class CarEngine:
def __init__(self):
self.type = "V8"
engine = CarEngine()
print(engine.type)
📤 Output: V8
✅ Example 3: Snake case for a function name
This example shows how to name a function that calculates a value using snake case.
def calculate_speed(distance, time):
result = distance / time
return result
speed = calculate_speed(100, 2)
print(speed)
📤 Output: 50.0
✅ Example 4: Mixing snake case and pascal case in a program
This example shows a class (Pascal case) with methods and variables (snake case) working together.
class DataProcessor:
def __init__(self):
self.raw_data = []
def add_record(self, record_value):
self.raw_data.append(record_value)
def get_average(self):
total = sum(self.raw_data)
count = len(self.raw_data)
return total / count
processor = DataProcessor()
processor.add_record(10)
processor.add_record(20)
processor.add_record(30)
average_result = processor.get_average()
print(average_result)
📤 Output: 20.0
✅ Example 5: Constants in snake case (all uppercase)
This example shows how engineers name constants that should not change during program execution.
MAX_CONNECTIONS = 100
DATABASE_TIMEOUT = 30
def check_connection(active_connections):
if active_connections > MAX_CONNECTIONS:
print("Too many connections")
else:
print("Connection limit okay")
check_connection(85)
📤 Output: Connection limit okay
Comparison Table: Snake Case vs Pascal Case
| Feature | Snake Case | Pascal Case |
|---|---|---|
| Example | user_name |
UserName |
| Used for | Variables, functions, methods, constants | Classes |
| Word separator | Underscore (_) |
Capital letter |
| First letter | Lowercase | Uppercase |
| All caps variant | Constants (e.g., MAX_VALUE) |
Not used |
When writing Python scripts, the names you choose for variables, functions, and classes matter more than you might think. Consistent naming makes your code readable, maintainable, and predictable for other engineers who may work with your scripts later. Python follows specific naming conventions outlined in PEP 8, the official style guide. The two most common forms you will encounter are Snake Case and Pascal Case.
⚙️ What Are These Naming Forms?
Naming forms define how you combine multiple words into a single identifier. Python uses different forms depending on what you are naming.
- Snake Case: All lowercase letters with words separated by underscores. Example: load_config_file
- Pascal Case: Each word starts with a capital letter, with no separators. Example: LoadConfigFile
There is also Camel Case (like Pascal Case but with a lowercase first letter), but Python rarely uses it. Stick to Snake Case and Pascal Case for clean, Pythonic code.
📊 When to Use Each Form
Python has clear rules about which form applies to which type of identifier.
| Identifier Type | Naming Form | Example |
|---|---|---|
| Variable names | Snake Case | server_count |
| Function names | Snake Case | get_disk_usage |
| Method names | Snake Case | connect_to_database |
| Module (file) names | Snake Case | network_utils.py |
| Package names | Snake Case (no underscores preferred) | netutils |
| Class names | Pascal Case | DatabaseConnector |
| Exception names | Pascal Case | ConnectionTimeoutError |
| Constant names | Uppercase Snake Case | MAX_RETRY_COUNT |
🛠️ Snake Case in Practice
Snake Case is the most common naming form you will use as a Python engineer. It applies to almost everything except classes and exceptions.
Variables hold data values and should describe their content clearly: - active_connections instead of ac - error_message instead of errmsg - total_disk_space_gb instead of tdsg
Functions perform actions and should start with a verb: - parse_log_file instead of log_parser - validate_ip_address instead of ip_valid - send_alert_notification instead of alert
Module files should be short and descriptive: - file_operations.py instead of fileOps.py - monitoring_tools.py instead of monTools.py
🏗️ Pascal Case in Practice
Pascal Case is reserved for classes and exceptions. This distinction helps you immediately identify when you are working with an object-oriented construct.
Classes represent blueprints for objects: - ConfigManager instead of config_manager - NetworkScanner instead of network_scanner - AlertHandler instead of alert_handler
Exceptions are special classes that indicate error conditions: - FileNotFoundError instead of file_not_found_error - ConnectionRefusedError instead of connection_refused - InvalidConfigurationError instead of invalid_configuration
🕵️ Common Mistakes to Avoid
New Python engineers often mix up these forms. Watch out for these pitfalls.
Using Pascal Case for variables or functions: - ❌ ServerName = "web01" - ✅ server_name = "web01"
Using Snake Case for classes: - ❌ class database_connector: - ✅ class DatabaseConnector:
Using mixed styles within the same script: - ❌ getUserData and parse_log_file in the same file - ✅ Stick to one style per identifier type
Using single-letter names (except in very short loops): - ❌ x = get_data() - ✅ response_data = get_data()
🧩 Quick Reference Cheat Sheet
Keep this simple rule in mind:
- If you are naming a thing that stores data → use Snake Case
- If you are naming an action (function) → use Snake Case
- If you are naming a blueprint (class) → use Pascal Case
- If you are naming a file → use Snake Case
- If you are naming a constant value → use UPPER_SNAKE_CASE
✅ Final Thoughts
Consistent naming is one of the easiest ways to improve your Python code quality. By following Snake Case for variables, functions, and modules, and Pascal Case for classes and exceptions, you align with the broader Python community. Other engineers will thank you when they read your scripts, and you will thank yourself when you revisit code you wrote months ago.
Interactive Views
You are currently in 📚 All-in-One mode. Use the tabs at the top to switch to 📖 Theory Only or 💻 Code Only views.
Naming standards define how engineers write variable, function, and class names in Python to keep code consistent and readable.
✅ Example 1: Snake case for a simple variable
This example shows the standard way to name a variable using lowercase letters and underscores.
user_name = "Alice"
print(user_name)
📤 Output: Alice
✅ Example 2: Pascal case for a class name
This example shows how to name a class using capitalized words with no underscores.
class CarEngine:
def __init__(self):
self.type = "V8"
engine = CarEngine()
print(engine.type)
📤 Output: V8
✅ Example 3: Snake case for a function name
This example shows how to name a function that calculates a value using snake case.
def calculate_speed(distance, time):
result = distance / time
return result
speed = calculate_speed(100, 2)
print(speed)
📤 Output: 50.0
✅ Example 4: Mixing snake case and pascal case in a program
This example shows a class (Pascal case) with methods and variables (snake case) working together.
class DataProcessor:
def __init__(self):
self.raw_data = []
def add_record(self, record_value):
self.raw_data.append(record_value)
def get_average(self):
total = sum(self.raw_data)
count = len(self.raw_data)
return total / count
processor = DataProcessor()
processor.add_record(10)
processor.add_record(20)
processor.add_record(30)
average_result = processor.get_average()
print(average_result)
📤 Output: 20.0
✅ Example 5: Constants in snake case (all uppercase)
This example shows how engineers name constants that should not change during program execution.
MAX_CONNECTIONS = 100
DATABASE_TIMEOUT = 30
def check_connection(active_connections):
if active_connections > MAX_CONNECTIONS:
print("Too many connections")
else:
print("Connection limit okay")
check_connection(85)
📤 Output: Connection limit okay
Comparison Table: Snake Case vs Pascal Case
| Feature | Snake Case | Pascal Case |
|---|---|---|
| Example | user_name |
UserName |
| Used for | Variables, functions, methods, constants | Classes |
| Word separator | Underscore (_) |
Capital letter |
| First letter | Lowercase | Uppercase |
| All caps variant | Constants (e.g., MAX_VALUE) |
Not used |