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

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?)

1

u/_pep_8_ Jan 02 '11

This is the second time that extremely long sentences are used to prove a point about readability when they don't fit within the programming context. And there probably is a good reason for this. And that is the "readability" really isn't a big issue at all with the types of names one uses within programming. Straw man like examples have to be used to prove underscores readability superiority.

You won't use a library if it has the wrong conventions. That is pretty extreme. Maybe you should find ways to remove parts of the standard Python library since it has so much hard to read CamelCase in it. Here's an example copy and pasted from the [unittest documentation] (http://docs.python.org/library/unittest.html)

import unittest

class SimpleWidgetTestCase(unittest.TestCase):
	def setUp(self):
		self.widget = Widget('The widget')

class DefaultWidgetSizeTestCase(SimpleWidgetTestCase):
	def runTest(self):
		self.assertEqual(self.widget.size(), (50,50),
						 'incorrect default size')

class WidgetResizeTestCase(SimpleWidgetTestCase):
	def runTest(self):
		self.widget.resize(100,150)
		self.assertEqual(self.widget.size(), (100,150),
						 'wrong size after resize')

Note the CamelCase class names are what the PEP says you should use. The method names in mixedCase, however, violate the PEP. Here's the apparently readable version for those who think underscores add so much. Note these are more realistic comparisons than writing 60+ character long full sentences.

import unit_test

class Simple_Widget_Test_Case(unittest.Test_Case):
	def set_up(self):
		self.widget = Widget('The widget')

class Default_Widget_Size_Test_Case(Simple_Widget_Test_Case):
	def run_test(self):
		self.assert_equal(self.widget.size(), (50,50),
						 'incorrect default size')

class Widget_Resize_Test_Case(Simple_Widget_Test_Case):
	def run_test(self):
		self.widget.resize(100,150)
		self.assert_equal(self.widget.size(), (100,150),
						 'wrong size after resize')

Personally I think the extra gaps created by the underscores's whitespace is undesirable in this context (vs reading English sentences). The emphasis between words takes away from groking the surrounding code by adding more distracting whitespace. The caps within the words are significant enough markers to improve the identifiers readability so I begin seeing identifiers as 1 unit in a larger statement parsed by whitespace instead of 2 or 3 or 4.

By the way, PEP8 calls the underscores with capital letters "ugly!" yet some making the underscore argument with sentences should see the whitespace gaps as desirable and more readable regardless.

0

u/[deleted] Jan 04 '11

[deleted]

0

u/_pep_8_ Jan 05 '11 edited Jan 05 '11

ohdeno:

uhm, PEP8 recommends CamelCase for Classes. Without underscores. So it'd be

class SimpleWidgetTestCase(unittest.TestCase): def set_up(self): self.widget = Widget('A widget')

But that's just cosmetics. This unittest module has much bigger problems than that. But I digress.

You have three upvotes for this? Seriously? You totally missed my point. First off: "uhm, PEP8 recommends CamelCase for Classes".

"Uhm", I already said that in the comment you are replying to

Note the CamelCase class names are what the PEP says you should use

Why are you repeating what I said? Maybe because you didn't understand what I was trying to do with the two examples pasted from unittest?

Let me summarize. First code example in my comment pasted directly from unittest documentation. Second is the same paste, but replacing it with the underscores to replace any times words run together (as opposed to CamelCase and mixedCase).

So what we learn from the first example is:

  • the code I pasted from the docs of the standard library violate PEP 8 rules and use mixedCase method definitions. So mcilrain might want to refrain from using these parts of the standard library like he does third party libraries.

  • That is really isn't that hard to read and identifier names in mixed case aren't these exaggerated long sentences that make CamelCase supposedly so hard to read.

What we learn from the second example is:

  • Underscores don't add much readability, but they do add a lot of noise

  • That the class names with "readability" underscores are described as ugly by PEP 8

There was no need for you to rewrite your own case. I already knew your case was the correct one via PEP 8 standards. You just totally missed the point I was trying to make.

In summary I'm not getting a lot of substantive responses to my comments. I just think its kind of ironic that mcilrain makes a comment "Python is supposed to be a language that's easy to read by humans." and that underscores are apparently a big part of that YET he uses the very example, HTTP, that PEP 8, uses to illustrate where you should use CamelCase and that he has to read CamelCase all the time since PEP 8 says you are supposed to use it for class names. He's using the wrong language because Python is not a pure "underscores everywhere for readability" language. And the bottom line is it doesn't have to be since underscores do little to add readability for identifiers that aren't full sentences except for non programmers who are just used to reading English and have never been exposed to the very common CamelCase and mixedCase naming conventions.

0

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

You have three upvotes for this? Seriously?

Well, technicaly, I have 2 upvotes.

You totally missed my point.

It seems you're not quite sure what your point is as well

Note the CamelCase class names are what the PEP says you should use

Rather conveniently quoted, wouldn't you say? Here's the context of what you've actually said:

Note the CamelCase class names are what the PEP says you should use. The method names in mixedCase, however, violate the PEP.

This is regarding the first paste. You can't possibly argue that this has any relation to your second paste, which you introduce in following paragraph.

Why are you repeating what I said? Maybe because you didn't understand what I was trying to do with the two examples pasted from unittest?

I didn't attempt to read your mind. I have, however, read your comment and commented upon what you have actually done with those two examples. You could argue that you did a poor job of explaining your intentions, but I think you were just trying to cover up your mistake in a convoluted way.

Let me summarize.

I don't think it means what you think it means.

  • the code I pasted from the docs of the standard library violate PEP 8 rules and use mixedCase method definitions.

And that is what you've said in the part you quoted. No objections here.

So mcilrain might want to refrain from using these parts of the standard library like he does third party libraries.

Not being PEP8 conformant isn't the only thing why you wouldn't want to use 'unit_test' module. I agree.

  • That is really isn't that hard to read and identifier names in mixed case aren't these exaggerated long sentences that make CamelCase supposedly so hard to read

You haven't argued that. Even if your second paste, the supposedly PEP-8 conformant one, would have been terribly unreadeable, that doesn't make CamelCase automaticaly the better choice. Those aren't even the only two choices.

And you didn't attempt to infer that from 1st example solely. You have used comparison. So that's part of your explanation is false as well.

What we learn from the second example is:

Condesending much?

What we learn from the second example is:

Once again you didn't analyze those two seperately. This is false.

  • Underscores don't add much readability, but they do add a lot of noise

That is if you have additional agent that is Capitalization. Underscores don't add "a lot of noise" if you use them how PEP-8 dictates.

  • That the class names with "readability" underscores are described as ugly by PEP 8

For a good reason. If you're already using camel case adding underscores would be unintuitive at best. PEP-8 isn't somehow suggesting that adding underscores in any context is a perfect readability elixir.

There was no need for you to rewrite your own case.

I have no idea what that means and neither had any one of my upvoters.

I already knew your case was the correct one via PEP 8 standards.

Nothing you have written in your previous comment seems to suggest that.

In summary (...)

See, that's what I mean.

I'm not getting a lot of substantive responses to my comments.

You have problems with writing and logical reasoning. That's why. I'm sure you have been told that already. This isn't personal, you should seriously work on that skills.

I just think its kind of ironic that mcilrain makes a comment "Python is supposed to be a language that's easy to read by humans." and that underscores are apparently a big part of that YET he uses the very example, HTTP, that PEP 8, uses to illustrate where you should use CamelCase and that he has to read CamelCase all the time since PEP 8 says you are supposed to use it for class names. He's using the wrong language because Python is not a pure "underscores everywhere for readability" language.

Oh my.

and that underscores are apparently a big part of that

@mcilrain argued that underscores are a better alternative to CamelCase. He wasn't arguing for underscores, but rather against CamelCase.

he uses the very example, HTTP, that PEP 8, uses to illustrate where you should use CamelCase and that he has to read CamelCase all the time

I don't understand that.

since PEP 8 says you are supposed to use it for class names.

From what I infer you're saying that 'http will always be http' isn't a valid point, since you have to make that distinction for class names anyway. That's allright and you could have said that in one sentence.

He's using the wrong language because Python is not a pure "underscores everywhere for readability" language.

That'd be consistent with my recent assertion.

And the bottom line is it doesn't have to be since underscores do little to add readability for identifiers that aren't full sentences

That's not relevant. Also you haven't demonstrate that in any way anyway.

except for non programmers who are just used to reading English

Not relevant. Also you didn't demonstrate how would underscores help "non programmers" (sic) who are "used to reading English."

have never been exposed to the very common CamelCase and mixedCase naming conventions.

So it's back to popularity? I agree with @mitsuhiko in his comment.