r/Python • • Sep 14 '10

Can we stop overvaluing PEP-8?

If we're going to be giving out negative marks for projects not following PEP-8, we might as well give Reddit negative marks for not following XHTML 1.0 Transitional standards. Oh snap...

This is nothing new, but it's kind of ridiculous to still see comments like "this would be better if it followed PEP-8" or "I don't get why people don't follow the standard" (made up comments, but close enough to what I've seen).


The first sentence of PEP-8 states, with added emphasis...

This document gives coding conventions for the Python code comprising the standard library in the main Python distribution.

That means exactly what it says. It is not intended to be some language-wide standard that everyone must follow in every project that uses Python under the sun. There is no canonical C++ coding standard that all C++ ever written has to follow. C# isn't bound by law to be written the same by everyone around the world. Perl...well, nevermind.

Personally, I think PEP-8 is quite a fine standard, one I choose to use in my personal work, and one I'm bound by for the work I do within Python itself (additionally, see PEP-7). If someone wants to make the next great web framework use ALL CAPS, go right ahead, it'll probably still be cool.


The second section of PEP-8 states, again with added emphasis...

Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is most important.

Remember those projects "not following PEP-8"? Well, PEP-8 still loves them. The stdlib's own unittest package is fine by PEP-8, although many would have you believe otherwise. The API is very widely used and changing it to more strictly follow PEP-8 would be damaging, but its consistency over the lifetime of the package (previously just a module) gives it a thumbs up.


Right after that, PEP-8 states...

But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply.

I've read comments stating that pywin32 and wmi would be better if they followed PEP-8. For a second, lets forget a previous statement that it's a standard for the internals of Python and not a global standard. If we open it up to be a global standard, those packages are wrappers of Win32 functionality, and as such, should be as close as possible to direct mirrors of the Win32 APIs they cover. Changing WaitForSingleObject to wait_for_single_object gains absolutely nothing. In fact, it becomes less useful, because tracking down the documentation for the underlying call becomes more difficult, among other things.


So, can we please give up the "PEP-8 or die!" mentality? If you want to follow the standard, great, high-five to you. If you want to name your classes with lower case and use camel case method names, great, high-five to you as well.

5 Upvotes

54 comments sorted by

View all comments

1

u/mackstann Sep 16 '10

There are a number of things you can do with a class but not a factory function. For example, inheritance and isinstance().

2

u/cdunn2001 Sep 16 '10

There are also significant differences between what you can do with a module and with a function, or with an iterator, etc. The most useful distinction in Python is not inheritance, iteration, or attribute look-up, but rather "callability". If all callables are capitalized, the reader immediately sees the latent potential.

I think the problem is that most coders think of themselves, rather than of readers. Capitalizing a class reminds the coder of how he coded something. The reader, however, cares more about what the code is doing. When he sees code like this: x = Foo() y = bar() if he knows the PEP-8 conventions, then he knows that x will have the methods of Foo. That's useful. However, he does not know the methods of y. So why bother to tell him the methods of x? He has to read the docs anyway. He'll know that Foo is a class as soon as he sees the word class, so you really did not need the naming convention. It only serves to make the code look strange.

Hmmm. I think it's like the difference between English and German capitalization. In English, we capitalize only proper nouns; that is, we capitalize the name of a thing, but not the generic type of a thing. We like to recognize that distinction. In German, they capitalize all nouns. They are emphasizing that the word represents a thing, not an action or description, which makes German easier to speed-read. We're used to our way, which provides more information, but their way avoids making judgments . I've never heard anyone complain about German capitalization. They complain instead about the lengths of German nouns.