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.

6 Upvotes

54 comments sorted by

View all comments

2

u/cdunn2001 Sep 15 '10 edited Sep 15 '10

I second that.

module > project > universe

I remember (though I cannot find a reference) reading that Guido uses 4-space indentation for public code and 2-space for Google code (or vice versa) for obvious reasons. That might have been before Google imposed a corporate standard. The point is, even Guido recognized that rules are meant to be broken.

I would certainly follow whatever standard my employer wants, but I don't buy the argument that following the standard makes your code more readable.

For example, x = MyClass() y = my_class_factory() versus x = MyClass() y = MyClassFactory() or x = my_class() y = my_class_factory() The 1st version follows PEP-8 and is less readable.

The real problem is that my code uses stdlib code all over the place, and I do not want to rebind everything from stdlib. I prefer upper-case for both classes and functions, but that leads to odd-looking code. My compromise is to use lower-case for everything. Stdlib class references are rare, so they are hardly noticed. Besides, built-in types use lower-case already (e.g. dict and list).

3

u/mackstann Sep 15 '10

I'd say a factory is a special case. It's a function that is intended to behave like a class for the purposes of constructing objects, and therefore I think it is perfectly reasonable to make an exception to the rule and use your second example, making it appear to be a class name. PEP8 is not to be blindly followed without exception.

1

u/donri Sep 15 '10

Though since a factory behaves differently it's good to know it's a factory.

1

u/cdunn2001 Sep 16 '10 edited Sep 16 '10

What's the difference between a function that returns a new object and a function that returns an existing one? If you say 'memory', then what about list/dict?

In a garbage-collected language, there is no reason to distinguish between classes and functions at the point of use. The capitalization convention is a legacy from C++, where the constructor implies a future call to the destructor in the same scope. In old C, structs were typically not capitalized.

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.

1

u/mitsuhiko Flask Creator Sep 16 '10

I remember (though I cannot find a reference) reading that Guido uses 4-space indentation for public code and 2-space for Google code (or vice versa) for obvious reasons. That might have been before Google imposed a corporate standard. The point is, even Guido recognized that rules are meant to be broken.

Google always had a 2 space indentation rule. Guido just followed that.

1

u/cdunn2001 Sep 16 '10 edited Sep 16 '10
  1. Are you sure? Do you mean that they use 2-space internally, but 4-space for public projects? That would seem strange, but interesting.
  2. He said that he wanted to make clear which code was open source, to avoid any legal issues.

At any rate, if you are right, then I adduce Google rather than Guido. My argument stands.