Python – Camel Case vs Snake Case

It's a question of style.
January 21, 2019

Someone recently asked me what is 'Camel Case', and what is 'Snake Case'? Which one is correct, and why? Why are some people so passionate about this subject? Is it really just a question of style?


Camel Case and Snake Case are two different styles of creating variable names in python. Consider for a few minutes the code example bellow.


# Examples of creating variable names in the CamelCase style.
LocalTaxRate = 0.06
ClientName = "John Smith"
GPSPosition = (43.294, -122.65 )
PassCode = "K48v7"

# Examples of creating variable names in the SnakeCase style.
local_tax_rate = 0.06
client_name = "John Smith"
gps_position = ( 43.294, -122.65 )
pass_code = "K48v7"

The Camel Case style [link] is a mix of upper and lower case letters, with uppercase letters used at the beginning of words. These uppercase letters form 'humps' in the string, that to some people, look like the humps of a Camel's back. This style is also sometimes called CapWords style, or Mixed Case style.

The Snake Case style [link] uses only lowercase letters and underscore characters, with underscore used to separate the words. Variable names created in this style are long and low, like a snake.

From a purely technical point of view, both styles are correct. They both follow the rules for variable names in python. A script written using either style will work equally well.

HOWEVER, There is a document called the 'Style Guide for Python Code', also know simply as the PEP-8 [link] . This document outlines many techniques for writing python code, and is considered to be the Gold Standard for doing so. Many large organizations like Google, Amazon, NASA, and the government, brag that they are fully PEP-8 complaint shops. Meaning that all the coders are expected to write PEP-8 complaint code. The PEP-8 brings order to chaos, especially on really large project where many people are contributing code. It prevents the project from falling into a wild hog-poge of different, and conflicting, coding styles.

And the PEP-8 says that the preferred style for variable names is…..Snake Case!

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

Variable names follow the same convention as function names.

MixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.

** From the 'Style Guide for Python Code' (PEP-8)

So, all of that being said, why would anyone want to write code using the Camel Case style?
Will, If you work in multiple programing languages like me, Camel Case style begins to make sense. You might want one style, which is easy to read, and works well across the different languages. By working in one style that works across the board, it is one less thing to keep in mind when moving between python, gambas, java, sql, PHP, C, etc.
Also, some people have told me that by using the Camel Case style, it is simply easier (keystroke wise) to type.
And for others, its simply a mater of working in their 'comfort zone'. Camel Case is the style that they used when they learn to code as a kid, and that is what they naturally go towards when given a choice. It's habit.
Besides, you can always do a global search/replace on your variable names at some later time, to bring your code into PEP-8 compliance.

A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is the most important.

However, know when to be inconsistent -- sometimes style guide recommendations just aren't applicable. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don't hesitate to ask!

In particular: do not break backwards compatibility just to comply with this PEP!

** From the 'Style Guide for Python Code' (PEP-8)

Some people will argue for, or against, a style of writing code with all the zeel of an overly caffeinated politician on a mad rant about tax reform law. They will say that you should ALWAYS code in the 'correct' style, so as to not develop 'bad habits'. They will then go into great detail as to why whichever style they favor is the 'correct' style. Most of these people seem to be in favor of the Snake Case style because of PEP-8 compliance.

Others will say that if you are working in a group/team, you probably should follow PEP-8. Otherwise, if it's your own private project, go ahead and express yourself by using whichever style you choose. They see writing code as an artistic expression rather than following fixed rules. No one every told Picaso what style he should paint in, so why should someone be told what style to write code in. Most of these people seem to be in favor of the Camel Case (sometimes called Mixed Case) style.

The 'Style Guide for Python Code' (PEP-8) covers a great many other python style subjects, not just variable names. It is well worth spending the time reading. The PEP-8 can be found at:

https://www.python.org/dev/peps/pep-0008/

So, the whole Camel Case vs Snake Case debate boils down to this:
Which style is acceptable at your place of work, and/or which style do you feel conferable with.
From a technical point, BOTH styles are correct.

It all comes down to a simple matter of style.

Last updated: 2019-01-21

Written by Joe Roten

Computer tech, Graphic Artist, Photographer, Writer, Educator, Programmer, Jack of many trades, Social gadfly, and Scholar without portfolio. http://www.gsw7.net/joe/

Written by Joe Roten

http://www.gsw7.net/joe/

As always

The information on my website is FREE.
But donations to help pay for Coffee and Beer are always welcomed.
Thanks.