Excerpt
Python utilizes the snake_case naming convention, rather than camelCase, due to its readability, consistency with PEP8 style guide, and widespread adoption.
Python has a strong culture of using snake_case
as the standard naming convention for variables, functions, modules and other identifiers. But why is this lowercase-with-underscores format favored over alternatives like CamelCase? In this post we’ll look at the history and rationale behind Python’s use of snake_case.
What is Snake Case?
Snake case, also known as underscores case, is a naming style where each word is separated by an underscore (_
) character. For example:
1my_variable
2some_function()
This differs from camelCase used in languages like Java where words are concatenated together without separators.
Origin of Snake Case in Python
The use of snake_case dates back to the early days of Python in the 1990s. The original Python code base made extensive use of this style, likely influenced by languages like C and Perl which also utilized underscores.
Guido van Rossum, Python’s creator, preferred snake_case due to its readability. Having visual separations between words made identifiers easier to parse at a glance. So the style stuck and was propagated through the many Python libraries and frameworks that followed.
Advantages of Snake Case
Beyond history and precedent, using snake_case for naming has several advantages:
Readability - The underscores visually separate words in identifiers, making them easier to quickly parse when reading code.
PEP8 conformance - Snake case is the recommended naming convention per the Python PEP8 Style Guide. Using it improves conformance with this widely used style guide.
Differentiation - Snake case differentiates Python from languages like Java and JavaScript which more commonly use camelCase. This helps maintain Python’s unique identity.
You can easily convert text to snake case online.
Wide Adoption of Snake Case
Given the above advantages, it’s no surprise snake case is used extensively:
The Python standard library utilizes snake case in all its module and function names
Major frameworks like Django and Flask also follow the convention
Most open source Python packages adhere to snake case as the de facto standard
This wide adoption makes snake_case the expected and preferred style for most Python developers.
Examples of Snake Case Names
Here are some examples of good snake case identifier names in Python:
1user_id = 453
2
3def get_user_name():
4 ...
5
6module_name = 'sys'
You can verify snake case names using this online converter tool.
Alternatives Like camelCase
While snake case dominates, some Python packages do use camelCase, especially for class names. For example:
1itertools.chain()
2
3json.JSONDecodeError
But overall camelCase is rare, and using it for things like variables and functions could harm readability.
Tools for Converting Names
If you need to convert names between styles, libraries like pygram can do so programmatically. There are also online tools like this snake case converter for quick one-off conversions.
Summary
To summarize, Python adopted snake_case early on thanks to its readability, and the style remains the standard convention. Following snake case improves PEP8 conformance and matches how the language is used by the broader community. While alternatives exist, snake_case remains the dominant, preferred approach to naming in Python code.