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

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.

1

u/_pep_8_ Jan 02 '11

Yes, the dash is better than the underscore in that it breaks up the excessive white space of the underscore.

adapting to the language you're using.

I wonder what you do when you use Jython? Or what about the threading module inspired by Java. Here's an example of what you will see in the docs:

Note: Starting with Python 2.6, this module provides PEP 8 compliant aliases and properties to replace the camelCase names that were inspired by Java’s threading API. This updated API is compatible with that of the multiprocessing module. However, no schedule has been set for the deprecation of the camelCase names and they remain fully supported in both Python 2.x and 3.x.

Examples:

  • threading.current_thread()

  • threading.currentThread()

  • threading.RLock()

  • threading.BoundedSemaphore([value])

  • class threading.Timer

  • threading.setprofile(func)

  • threading.stack_size([size])

1

u/akdas Jan 02 '11

I wonder what you do when you use Jython?

That's a very good point. At some point, you will have mixed conventions, since you'd be using some functionality in Python's standard library and some functionality in Java's. Which style you'd use for code you write is tougher to decide. Perhaps it's a matter of whether you view the particular application as a Python program utilizing the Java's libraries, or as Java program that uses Python's syntax.

Or what about the threading module inspired by Java.

I'm not sure of the exact circumstances surrounding that, but it seems like a good idea to go with the PEP 8 style names, and it seems like something that should have been done from the beginning. After all, it is inspired by Java, but it isn't Java. (There's probably more to the story than that, so correct me if that's the case.)

1

u/_pep_8_ Jan 02 '11

I know its not Java and I am thankful its not! However, if you are going to use Java's CamelCase for class names, why not just go all the way. You've already introduced identifiers not separated by underscores.

Given Java's popularity there really is no point in deviating from a naming convention that works and it almost ubiquitous. People coming over from the language who know some of the libraries inspired by Java don't have to worry about mistyping a method name because its in underscores instead of mixecCase.

The people criticizing CamelCase and mixedCase as much more unreadable are really making a subjective argument. Its more readable to them. Maybe they can work their whole lives programming in Python, but even Python uses CamelCase so you have to get used to it. And widely used languages like Java and Javascript (libraries too like jQuery) and C# also use it.

Once you know your identifiers you probably aren't reading the whole thing anyway. Your brain probably sees the first few letters and knows what it is. The extra gaps created by underscores is just a distraction at that point disrupting your ability to grok the surrounding syntax. Your hands will also thank you for it.

0

u/[deleted] Jan 04 '11 edited Jan 04 '11

Actually I don't see why threading.current_thread is even a callable. Maybe it's just a design decision, but there's no reason why threading.current_thread shouldn't be a property. Furthermore calling this function doesn't have any side-effects anyway.