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.

1 Upvotes

56 comments sorted by

View all comments

0

u/tesseracter Jan 02 '11

can't downvote you, but i dislike the cut of your jib. my idea of good looking python code is not being able to tell a 99 bottles of beer script from a neural network on the shape of the code alone -- it all looks neat, clean, and plain. leave all the complicated stuff to ruby/perl folks who think they are 'code ninjas/rockstars' and allow me to go in and use your python code in a super boring and productive matter. this means following pep, and therefore getting the magical ability to guess function names without looking at your source code, and having it be intuitive enough to just get it right.

1

u/_pep_8_ Jan 02 '11 edited Jan 02 '11

I'm not sure you are addressing me directly or maybe you meant to reply to someone else commenting here? This code ninjas/rockstars comment seems far out in left field to me.

I agree with almost 99% of what the PEP says. I try my best to follow it. I dislike the recommendation to move forward with underscores and the mix of choices I like I'm seeing in the threading module inspired by Java. Personally, I think the PEP should have stated something to the effect, that when using all lowercase makes a long, unreadable name either use an _ to separate parts of this name or capitalize the words within it like Java.

People declaring there is some huge problem with readability with mixedCase and that underscores are superior and maybe happy the PEP suggest this are probably missing the fact that Python follows the Java CamelCase convention for naming classes.

The PEP makes this statement

Capitalized_Words_With_Underscores (ugly!)

yet I don't see a huge difference between that and this: capitalized_words_with_underscores.

If Java was written with underscores all over the place we could probably complain it looked more convoluted and verbose than it already does. Because the underscores create noise and extra whitespace where whitespace is significant and distracting when looking at a program.

2

u/tesseracter Jan 02 '11

python actually uses TitleCase, not camelCase. small difference, but one looks better than the other.

the rockstar comment was just because i hate working with folks that think their personal style is the awesomest, and trumps convention.

1

u/_pep_8_ Jan 02 '11

No, I got it wrong. Check the PEP. It explicitly declares CamelCase the same as CapitalizedWords. It calls what I called camelCase, mixedCase.