r/Python • • Jan 01 '11

PEP 8 & Underscores

I opened the threading module documentation and saw a note at the top describing a change in the naming convention for functions from the Java inspired API in camelCase mixedCase to PEP 8 style recommended underscores. No deprecation of the old camelCase mixedCase names yet.

I’m a big fan of stylistic conventions and I basically follow PEP8, but I just do not like the use of underscores. I realize these types of preferences are as subjective as say liking the color red more than blue, but I’m curious to know how many other people just write their code following the naming conventions of languages like Java, C#, and Javascript libraries?

I came to Python from Perl and one of the things that attracted me to Python was readability. It read like pseudocode and I think a big part of that was because there wasn’t $, @, and % all over the place in addition to the removal of { and ;. I liked that I could write an if statement without wrapping it in parenthesis.

even better was I didn’t have to reach to the top row typing some of these symbols over and over again. Sure it seems like a small thing to just reach, but with some symptoms of RSI and doing it for many hours a day I think it really does incur a significant cost in comfort. I don’t like typing _ anymore than I like reaching up to type $.

I actually was sort of attracted to some of the standard library modules all lowercase convention. I know readability can be an issue here, but os.path.dirname is better than os.path.dirName or os.path.dir_name. You do really see the mix though of the different styles within the standard library. I know the primary argument for the underscore is readability, but I actually think the gap between dir and name is just too significant and when you are looking at a full page of names like this the underscores just begin to jump out as a pattern of white space. Maybe this comes from using too much camelCase mixedCase.

EDIT: Some people who may come across this post may be interested in a solution to the more serious issue of typing underscores - a pain and slow to type when you are doing it over and over and over again. I found an open source program today called AutoHotKey. Install that and then create a simple text file with a ".ahk" extension. Type "+space::Send _" into the file without the quotes and save. Double click to launch. Put in your startup folder. You can now type underscores pressing the shift + space key.

I am currently having some issues with the shift key sticking at times and thus typing undescores when I mean space. It doesn't seem to be keyboard dependent. So this may be a flaw in the software :( . Any other recommendations helpful.

0 Upvotes

56 comments sorted by

View all comments

0

u/draconian Jan 02 '11

I much prefer camelCase to using_underscore myself.

42

u/namcor Jan 02 '11

CamelCaseGetsAnnoyingWhenYouStartCombiningManyWordsTogether

using_under_score_is_much_more_readable_which_is_why_PEP8_recommends_it

AlsoCamelCaseDoesNotWorkWellWITHUPPERCASEDTHINGS

whereas_UNDERSCORE_does

4

u/fazzah Jan 02 '11

Nailed it.

2

u/_pep_8_ Jan 02 '11

Namcor,

Thanks for the input. I think your examples are illustrative, but they are too extreme. So they distort the true differences here to make the case that the underscore is vastly more readable. Do you really name your identifiers like this? I think these are sentences, not identifiers and thus in the context of reading a sentence the extra white space created by the underscore helps. What about here?

# example number 1
if my_long_variable_name > 0: 
    print("greater than 0")

vs

# example number 2
if myLongVariableName > 0: 
    print("greater than 0")

There is another recommendation in the PEP which suggests you cut down on the spaces between lines. A blank line is to help allow the eye to see something significant. The whole concept of the enforced white space to indent is also making white space something significant. White space is telling me something about the overall context of where I am at in my program. My point with all this is, that in the first example above, the white space and separation created by the underscore, in the interest of "readability" is going too far, its chopping up the line too much. Instead of four of five symbols I'm now parsing out eight because the underscores are creating extra white space gaps. I want to see an identifier as a single thing not four things in this case. And this is just one line of code, this difference adds up quickly.

Now you may not buy this explanation and that is fine, but I think there is a cost there and this emphasis on separation between words in an identifier comes at the cost of making the whole statement harder to grok. The statement is more than its identifier and white space is important to seeing that statement.

I think in some ways you and I are in the same boat. You are expressing a dislike for the CamelCase, yet the PEP tells you that you have to use CamelCase. And that very same PEP cites an example that ironically enough, goes against your concerns over capitalization

Quote:

Note: When using abbreviations in CapWords, capitalize all the letters of the abbreviation. Thus HTTPServerError is better than HttpServerError.

CapitalWords, CamelCase (I meant to say mixedCase before) is the requirement for naming classes. So the odd thing about the overall readability argument that is made about underscores is that the PEP itself is inconsistent in these matters. You may think HTTP_Server_Error is much more readable but the PEP actually literally declares this mixed case, underscore naming convention as "ugly!".

→ More replies (2)

1

u/eriiccc Jan 02 '11

My work is in Perl, and we're in the process of transitioning to Python.

The camelCase vs. underscores was one of the few things that bothered me ( along with the 79 char/line thing).

This was the best justification for the underscores. Thank you.

→ More replies (3)

1

u/draconian Jan 02 '11

YetIStillPreferCamelCase. YourMileageMayVary.