Should I Use Underscores or Camel Case for Python?

Should Python use underscore or camel case naming conventions? This explores in detail the pros and cons of each for readability and maintainability.
On this page

Should I Use Underscores or Camel Case for Python?

Excerpt

Learn about the pros and cons of using underscores or camel case for Python programming. Understand the importance of choosing the right naming convention for readability and maintainability of your code.


Python’s naming conventions can be confusing for beginners. When naming variables and functions, should you use underscores like variable_name, or camel case like variableName? This article will explain the key differences between these two styles and offer recommendations on when to use each one.

Introduction

In Python, underscores and camel case are two common naming conventions used for variables and functions.

The underscore format uses underscores to separate words in a name, like first_name or calculate_average(). This is sometimes called snake case.

Camel case capitalizes the first letter of each word and removes spaces, like firstName or calculateAverage().

So which naming convention should you use for Python? Read on to see the pros and cons of each format.

Brief Background on Python Naming Conventions

Python’s official style guide (PEP 8) recommends using underscores to separate words in variable and function names. This matches the naming conventions used in Python’s standard library.

However, some Python programmers prefer camel case, especially those with experience in other languages like Java, JavaScript, and C# that commonly use camel case.

So while underscores are more “Pythonic”, camel case is still frequently used.

Reasons to Use Underscores

Here are some of the benefits of using underscores for variable and function names in Python:

  • More readable: Many find underscores easier to read than camel case. Words separated with underscores are more distinct.
  • Follows PEP 8 recommendations: Underscores align with Python’s official style guide for readability.
  • Matches Python standard library: The Python standard library uses underscores in names like itertools.chain(), setting a precedent.

For example:

1# Underscore example
2first_name = "Mary"
3
4def calculate_average(numbers):
5  total = sum(numbers)
6  return total / len(numbers)

So if you want to follow Python conventions, underscores are the way to go.

Reasons to Use Camel Case

However, there are also some good reasons to use camel case in Python:

  • Common in other languages: If you have experience with Java, JavaScript, C#, etc, you’re probably used to camel case already.
  • Capitalization of class names: Class names are capitalized like MyClass in Python, so camel case fits this convention.
  • Avoids issues with reserved words: If you use an reserved word like class as a variable name with underscores, it may cause issues.

For example:

1# Camel case example
2firstName = "Mary"
3
4def calculateAverage(numbers):
5  total = sum(numbers)
6  return total / len(numbers)

So if you’re used to camel case or want to capitalize class names, it can still be a good option.

Free Online Conversion Tools

Switching between underscores and camel case can be tedious to do manually. Luckily, there are some great free online tools that can instantly convert between the two formats:

For example, pasting calculate Average into the snake case tool returns calculate_average.

When to Use Each Style

Given both styles have their merits, when should you use each one for Python? Here are some best practices on naming conventions:

  • Use underscores for functions and variables - Matches Python style guidelines
  • Use camel case for class names - Allows capitalized names like MyClass
  • Aim for consistency within a given project - Avoid mixing styles

Keeping these recommendations in mind will help you write Python code that is easy to read and follows conventions.

Pros and Cons of Each Style

To summarize, here are the main pros and cons of underscores vs camel case for Python:

Underscores

Pros:

  • More Pythonic/idiomatic
  • Easier to read
  • Follows PEP 8 style guide

Cons:

  • Not as common in other languages

Camel Case

Pros:

  • Familiar to users of other languages
  • Allows capitalized class names
  • Avoids reserved word issues

Cons:

  • Less “Pythonic”
  • Can be less readable than underscores

Neither naming convention is inherently better or worse than the other. It comes down to balancing readability, convention, and personal preference.

Recommendations and Best Practices

Based on all of the above points, here are my recommendations on using underscores vs camel case for Python:

  • Use underscores for variable and function names - This is the more “Pythonic” style and matches PEP 8 guidelines. For example: number_of_students or calculate_average().

  • Use camel case for class names - Class names are capitalized in Python, so camel case fits this convention well. For example: StudentDatabase.

  • Prioritize consistency within a project - Pick one style and stick with it across all files in a project. Mixing underscored and camelCased names can be confusing.

  • Use online conversion tools - Easily convert between styles with online tools like String to Snake Case and String to CamelCase.

Following these best practices will help you write clean, readable Python code.

Conclusion

While Python officially recommends using underscores for variables and functions, there are situations where camel case can also be appropriate. The most important things are maintaining consistency within a project and maximizing readability.

If you’re new to Python, get in the habit of using underscores as it is considered more “Pythonic”. But don’t avoid camel case entirely, especially for class names.

Take advantage of online tools to convert between naming styles, but try to avoid mixing conventions within one codebase. Following Python’s naming best practices will make your code easier for others to read and understand.