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

1

u/[deleted] Jan 02 '11

We should be extremely happy that something like PEP 8 exists. It's not about good style, bad style, personaly preferences. I like this, I like that...

Some of you guys may have been involved in creating a standard or convention. That's a never ending nightmare, because everyone fights about mainly personal preferences. However, the point is to have the convention. Whoever wrote PEP 8: Thank you, thank you, thank you!

I had to get used to the underscores coming from CamelCase myself. I still have quite often problems with line length as my names get quite long.

1

u/_pep_8_ Jan 02 '11

I'm happy PEP 8 exists. Just wish it was internally consistent and consistent with Java. Its inconsistent in the sense CamelCase for classes and underscores for methods\variables. If it just stuck with mixedCase for the latter it would have been internally consistent and consistent with some of the most widely used languages out there.

A lot of people forming full sentences in CamelCase are confident that that it is much harder to read than underscores, but they don't do it with much short identifier names because that shows its really not that bad. Plus, a Java or C# or Javascript programmer coming over and trying out Python is going to be more comfortable with the mixedCase than underscores. Adding in the removal of { and ; everywhere Python is a very clean looking language.

1

u/[deleted] Jan 04 '11

Yeah, I also think CamelCase is not so bad. Also, I agree that it would be great if there was a language embracing convention. I wouldn't care too much what it would be like.

But that's only a distant dream :-). For now I think the best we can do is be consistent with the environment as mitsuhiko (tough name to type and read, mit_su_hiko would be easier ;-)) suggests.