Why Does Python Use snake_case as a Convention?

Python standardly uses snake_case naming with underscores for variables and functions. Learn why this style became the convention.
On this page

Why Does Python Use snake_case as a Convention?

Excerpt

Python adopts snake_case naming as standard convention. Learn why this style was adopted and its readability advantages.


If you’ve written Python code, you’ll notice a strong convention - variables, functions, and attributes use snake_case with underscores between words. But why did this style become the standard in Python? Let’s examine the history and advantages of Python’s snake_case naming.

What is Snake Case?

Snake case (also called underscore case) is a naming style that uses lowercase letters with underscores between words. For example:

1first_name = "Mary"
2user_id = 4562

It provides visual separation between words for improved readability.

Python code overwhelmingly uses snake_case by convention for variables, functions, methods, and module names.

History and Origins

Python’s convention of snake_case was influenced by other languages:

  • C used underscores between words in names
  • Lisp code used underscores as word separators
  • Ada and Modula also adopted underscores in names

So when Python was created in 1991, using underscores alignged with existing languages that influenced its design. This lowercase and underscore style became the standard convention as Python evolved.

Advantages of Snake Case

Using snake_case for names in Python code has some notable advantages:

  • Improves readability with visual separation of words
  • Avoids confusing words that run together in camelCase
  • Differentiates names from Classes which use CapWords
  • Consistency across operating systems and file systems

The readability and differentiation snake_case provides led to its widespread adoption.

Disadvantages of Snake Case

There are a couple drawbacks to snake_case to consider:

  • Can be cumbersome to type many underscores
  • Not as compact as camelCase or PascalCase

But the improved readability has far outweighed these minor disadvantages.

PEP 8 Styles

Python’s official style guide PEP 8 strongly recommends using snake_case for variables, functions, methods, and modules. PEP 8 discourages the use of camelCase style.

Exceptions

Some exceptions where snake_case may not be used include:

  • Following external library conventions
  • Overriding default naming styles
  • Interfacing with other languages

So snake_case can be overridden, but underscores remain the standard convention.

Automated Formatting

Tools like Black auto-format Python code and will enforce snake_case by default. This automates adherence to the standard naming conventions.

Conclusion

Python adopted the snake_case naming style early on under influences from languages like C and Lisp. This convention stuck and is now engrained as idiomatic Python code. The naming improves readability and avoids clashes.

There are some exceptions based on external libraries. But wherever possible, using snake_case for names provides consistency and makes Python code more Pythonic.