Introduction
Python, like any other programming language, has its own set of rules and conventions when it comes to naming variables and functions. These conventions aren't just for aesthetics or to make your code look pretty, they serve a much more important role in making your code more readable and maintainable. If you've read many of my articles on StackAbuse, I talk a lot about writing readable code. By following Pythonic best-practices in naming and formatting your code, you'll make it much more readable for others (and yourself).
In this article, we'll explore the different naming conventions used in Python and understand why they matter.
Why Naming Conventions Matter
Imagine working on a large codebase where variables and functions are named/formatted haphazardly. It would be a nightmare to understand what each variable or function does, let alone debug or add new features. This is one of the reasons why we put so much emphasis on following conventions.
Naming conventions are basically just agreed-upon standards that programmers follow when naming their variables, functions, classes, and other code elements. They provide a level of predictability that makes it easier to understand the purpose of a piece of code. This is especially important when you're working in a team.
Following naming conventions isn't just about making your code understandable to others. It's also about making it easier for your future self. You might understand your code perfectly well now, but you might not remember what everything does six months down the line.
Variable Naming Conventions
In Python, variable names are more than just placeholders for values - they are a vital part of your code's readability. Python's variable naming convention is based on the principle of "readability counts", one of the guiding philosophies of Python.
A variable name in Python should be descriptive and concise, making it easy for anyone reading your code to understand what the variable is used for. It should start with a lowercase letter, and it can include letters, numbers, and underscores. However, it cannot start with a number.
Here are some examples:
name = "John Doe"
age = 30
is_student = False
Note: Python is case sensitive, which means age
, Age
, and AGE
are three different variables.
In Python, we commonly use snake_case
for variable names, where each word is separated by an underscore. This is also known as lower_case_with_underscores
.
student_name = "John Doe"
student_age = 30
is_student = False
Function Naming Conventions
Like variable names, function names in Python should be descriptive and concise. The function name should clearly indicate what the function does. Python's naming conventions for functions are similar to its conventions for variables.
In Python, we typically use snake_case
for function names. Here's an example:
def calculate_sum(a, b):
return a + b
result = calculate_sum(5, 3)
print(result) # Output: 8
Note: It's a good practice to use verbs in function names since a function typically performs an action.
In addition to snake_case
, Python also uses PascalCase
for naming classes, and occassionally camelCase
, but we'll focus on those in another section. For now, remember that consistency in your naming convention is important for to writing clean, Pythonic code.
Class Naming Conventions
For naming classes in Python, a different set of conventions applies compared to naming variables or functions. In Python, class names typically use PascalCase
, also known as UpperCamelCase
. This means that the name starts with an uppercase letter and has no underscores between words. Each word in the name should also start with an uppercase letter.
Here's an example to illustrate the naming convention for classes:
class ShoppingCart:
def __init__(self, items=[]):
self.items = items
def add_item(self, item):
self.items.append(item)
my_cart = ShoppingCart()
my_cart.add_item("apple")
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
In this example, ShoppingCart
is a class that adheres to the PascalCase
naming convention.
Note: While function names often use verbs to indicate actions, class names usually employ nouns or noun phrases. This is because a class often represents a thing or a concept rather than an action.
Sometimes you'll encounter classes that contain acronyms or initialisms. In such cases, it's conventional to keep the entire acronym uppercase:
class HTTPResponse:
def __init__(self, status_code, content):
self.status_code = status_code
self.content = content
Just like with functions, the key to good class naming is to be descriptive and concise. The name should clearly convey the class's purpose or functionality. And as always, maintaining consistency in your naming conventions throughout your codebase is vital for readability and maintainability.
Conclusion
In this article, we've explored the importance of naming conventions in Python, and how they contribute to code readability and maintainability. We've showed the different types of naming conventions for variables, functions, and classes, like PascalCasing
and snake_casing
.
Python does not enforce these conventions, but adhering to them is considered good practice and can really improve your code's readability, especially when working in teams.