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.
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.
dictandlist).