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

Show parent comments

2

u/JimH10 Jan 02 '11

camel case saves a character a word

Yes, I came here to say this. Putting a line f code on a single line is typically better than on multiple lines

which can easily add 3 characters to the line

author_name_list = [author_name for author_instance.author_name in author_instance_list]

is a lot of spaces, including the two around the equal sign

Not exactly game breaking

But if the line is already indented by a dozen spaces, it can easily cause you to run over onto the next line (especially if the line is commented).

Still, I generally follow PEP 8, although those parts I consider suboptimal.

1

u/hippocampe Jan 02 '11

should be written:

author_names = [author.name for author in authors]

... these 'instance' and 'list' have no place.

2

u/JimH10 Jan 02 '11

... these 'instance' and 'list' have no place

Your opinion. Mine differs. I find them useful sometimes, particularly when revising the code weeks later.

In any event, it is a made-up example illustrating that there can be lots of spaces taken up on a line. My point remains correct.

0

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

Maybe it's opinion like you say but I have some suggestions:

author_instance

You already make distinction between ‘Author’ class (capitalized, camel-case) and ‘author’ instance (lowercase). Having ‘instance’ suffix seems redundant.

object_list vs objects

This seems like unnecessary implementation detail. Why do you care whether your collection is list, set or generator? I can see how in Python 3 (or Python 2.5+) when generator is the default, having an explicit in-memory data structure is worth noting, however in that example it really doesn't matter.

Actually I'd argue even that ‘author_names’ is redundant. Simply

names = (author.name for author in authors)

should be sufficient. ‘author_names’ makes sense only if you have other names in given context (bird names, elephant names, socks names). And perhaps it's better to have smaller context rather than having explicit names.

1

u/JimH10 Jan 04 '11

Again, you seem not to have understood the point.

The point is that using underscores can add significantly to the length of a line, over using camel case.

As to the things you say about the code, in the first place it was an off the top of my head example, not an excerpt. In the second place, the code I had been working on earlier actually has two lists of names, so I need to distinguish between author_names and package_names. In the third place, I have a practice that when I have to revisit code to debug it a number of times (because the testing is exposing errors), I make the parts very explicit, anticipating that I may have to look at it weeks from not when another bug gets noticed and I remember less what the parts are.

But all that is neither here nor there. The point is that in an 80 character line, which may well be indented 16 characters, underscores can occupy 10 characters.

0

u/[deleted] Jan 05 '11

Again, you seem not to have understood the point.

Again?

The point is that using underscores can add significantly to the length of a line, over using camel case.

I didn't comment on that.

As to the things you say about the code

That is the actual things I say.

in the first place it was an off the top of my head example, not an excerpt

So you don't normally use 'instance' suffix?

the second place, the code I had been working on earlier actually has two lists of names, so I need to distinguish between author_names and package_names.

I did acknowledge that.

I make the parts very explicit, anticipating that I may have to look at it weeks from not when another bug gets noticed and I remember less what the parts are.

That's fair but I'm not sure on how obfuscating your variables is helping that.

But all that is neither here nor there. The point is that in an 80 character line, which may well be indented 16 characters, underscores can occupy 10 characters.

That's hardly the case if you are not in habit of making your variables unnecessary long. I'd argue that if you're ever near 80 chars line limit then you must be doing something wrong.

Still, this is not what I was commenting on before.

0

u/JimH10 Jan 06 '11

Apology accepted.