Junior
What are the best practices for choosing variable names in Python?
sobes.tech AI
Answer from AI
- Use descriptive names that reflect the purpose of the variable.
- The length of the name should be proportional to its context (short names for temporary variables, longer for global ones).
- Use lowercase with snake_case for regular variables and functions.
- Use uppercase with snake_case (UPPER_SNAKE_CASE) for constants.
- Use CamelCase (UpperCamelCase) for class names.
- Do not use Python reserved words.
- Avoid single-letter names, except for obvious cases (
ifor loops). - Be consistent in naming throughout the project.
- Use prefixes if it improves readability (e.g.,
is_active,has_items). - Avoid using overly long names that hinder readability.
# Good names
user_name = "Alice"
total_count = 100
is_valid = True
MAX_RETRIES = 5
class UserProfile:
pass
# Bad names
x = "Alice" # Not obvious that this is a username
tc = 100 # Not clear what this counter is
valid = True # Better to add a prefix 'is_'
retry_count = 5 # Not a constant