Should I Use Underscores or Camel Case for Python?

Python's official style guide Pep 8 recommends using underscores and snake_case for variables and functions. But camelCase is also used sometimes.
On this page

Should I Use Underscores or Camel Case for Python?

Excerpt

For idiomatic Python code, the standard convention is to use underscores and snake_case for naming variables and functions. But camelCase is acceptable in some cases like integrating with other languages.


When naming variables and functions in Python, developers have two main formatting options - underscores (snake_case) or camelCase. But which casing style is preferred for Python? In this post we’ll compare the pros and cons of underscores vs camel case.

Introduction

First, let’s define these two formatting approaches:

  • Snake case (underscore) - example_variable
  • Camel case - exampleVariable

The official Python style guide Pep 8 recommends using snake_case for variables and functions. But you’ll also see some camelCase, so which is better?

Advantages of Underscores in Python

Using underscores for variable and function names has several advantages:

  • More “Pythonic” coding style
  • Improves readability with visual separation
  • Distinguishes names from Classes which use CapWords
  • Consistent conventions across platforms

The underscores help make code more idiomatically Python.

Disadvantages of Underscores

Potential downsides to underscore naming include:

  • Can be awkward to type long names with underscores
  • May be difficult to quickly parse visually
  • Not as compact as camelCase

So while readable, underscores require more typing and can appear messy.

Advantages of Camel Case

Camel case offers some benefits like:

  • Natural capitalization is familiar
  • Compact to type without extra symbols
  • Used heavily in other languages like JavaScript

So it does allow for dense, compact variable names.

Disadvantages of Camel Case

Downsides to using camelCase include:

  • Not the common convention in Python
  • Reduces visual separation between words
  • Could conflict with ClassNames

It’s not considered very “Pythonic”.

Automated Formatting

Tools like Black auto-format Python code. Black enforces snake_case naming by default. But it can be configured to allow camelCase with:

1# pyproject.toml
2
3[tool.black]
4skip-string-normalization = true

So auto-formatters let you enforce naming styles.

When to Use Camel Case

There are some cases when camelCase may be appropriate:

  • Interfacing with libraries in other languages
  • Following conventions of a Python library
  • Overriding default styles

So it’s not strictly forbidden, but underscores are strongly preferred.

Conclusion & Recommendations

For most Python coding, using snake_case for variables and functions is the standard convention and recommended approach. But camelCase is sometimes used, especially when integrating with other languages.

My recommendation is to stick with snake_case by default for idiomatic Python. Use auto-formatters like Black to enforce this. Only use camelCase when specifically interfacing with external libraries that require it.

Consistency in naming really helps improve code readability and quality. Follow Python’s underscore conventions and your future self will thank you!