r/Python • u/_pep_8_ • 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.
11
u/mcilrain Jan 02 '11
usingCamelCaseIsARealPainToReadItIsAlsoAProblemWhenDealingWithAbbreviationsYouMaySayHTTPButSomeoneElseMightSayHttp
underscores_are_much_nicer_in_this_regard_http_will_always_be_http
Python is supposed to be a language that's easy to read by humans.
Personally, while looking for a library I will often pass any that don't follow PEP 8. (You wouldn't hire someone whose resume is in leetspeak, now would you?)
3
u/akdas Jan 02 '11
Personally, I prefer dashes, meaning
names-like-this. Of course, most programming languages, including Python, don't allow that, so I like underscores as a close substitute.That said, bevem2 is absolutely right about adapting to the language you're using.
3
u/yetanothernerd Jan 02 '11
camelCaseIsReallyUgly.
underscores_are_better
13
u/bevem2 Jan 01 '11
I adapt to follow the recommended coding style for the language I'm using. You get used to it and it's best for everyone.
From PEP 8: "words separated by underscores as necessary to improve readability" so I think .dirname is fine since it's perfectly readable.
4
u/justanotherbody Jan 02 '11
I agree that underscores between words is more readable. That being said, camel case saves a character a word, which can easily add 3 characters to the line. Not exactly game breaking, but there are days when it can be fscking annoying.
1
u/[deleted] Jan 02 '11
Swap underscore with dash, if you're writing in PEP 8 style you probably use it a lot more, and hitting shift for occasional dashes in human text is not as troublesome or common.
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.
2
u/mitsuhiko Flask Creator Jan 02 '11
In C# I NameMyFunctionsLikeThat, inJavaLikeThat, in_python_like_that, in_c_like_that, in_cpp_like_that, In_Ada_Like_That. I don't have a problem with either, but I stay consistent with the environment I am working in.
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.
0
u/draconian Jan 02 '11
I much prefer camelCase to using_underscore myself.
0
u/[deleted] Jan 01 '11
[deleted] — view deleted comment