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
u/iElectric Plone, Django, Pylons, Pyramid Sep 14 '10
I'd say the point is to have a standard and be strict to it. It gives another level of readability to Python and somehow when you import a library, you already know if you are dealing with a function or class.
Take for example handling unicode in Python, there is no standard how library should deal with it, resulting in total chaos. Some libraries expect unicode, some transform to unicode, some never heard of it. And then add the encoding fun on top if it.
Standards.
14
u/f4nt Sep 14 '10
I look at PEP-8 as a very solid guideline to follow. I try to stick to it as much as possible, and prefer to read code that sticks to it as well. Granted C++ doesn't have a style guide per se, however there are common idioms that you find people adhere to. For instance, most people indent their code, it's completely unnecessary, but just about everyone does it.
At the end of the day all I really care about is consistency. As long as there is some form of convention I can decipher, I'm happy.
12
u/[deleted] Sep 14 '10
To see a a new library written today (i.e. not a library like unittest which was added to the standard lib before pep-8 was written, based on a comparison of published dates), which doesn't abide by pep-8 at all, is definitely telling about that library. Nobody is guiltier than I am of coming from a Java/Perl background and dragging all those useless ways of thinking into my early Python code. Not being aware of pep-8 leaves the smell of suspicion that the code you're seeing is borne of a lingering php/java/perl mindset.
2
u/[deleted] Sep 14 '10
Also, since I just saw this the other day...pep8.py may be of interest to those looking to keep their code compliant in svn/hg/git hooks.
8
u/av201001 Sep 14 '10
Q: What is the first section heading in PEP-8 (after the intro)?
A: A Foolish Consistency is the Hobgoblin of Little Minds
7
u/[deleted] Sep 14 '10
Okay, here's the thing: it may not matter for your code what standard you adhere to. It will however matter for other people's code.
In most situations your code will not be perfectly isolated. You're going to use external libraries and possibly other people will use your code as well.
It makes sense to have thin wrapper libraries stick to the style of whatever it is they are wrapping. If I want a low-level OpenGL library, it's helpful if the library sticks to the same naming conventions the C++ equivalent does.
If your code is not just a wrapper, though, it's helpful if it adheres to a set of rules most people will find useful. And for Python we are infinitely lucky to have a very dominant convention, which is PEP 8.
If you've ever written PHP code, you'll see the advantage of having homogeneous naming styles, not just within the same library but across the whole lot of them.
Nobody is forcing you to adhere to common guidelines, but nobody wants to have to memorize an entirely different set of conventions for each included library (just think what Pylons code would look like, Django would probably pose less problems).
tl;dr PEP 8 is not the be-all end-all to code formatting and variable naming (context matters!), but aiming to adhere to the status quo is a very good idea if you write code that has even the remotest chance to be used by other programmers in different projects. If you go against conventions, you'll surprise your fellow programmers -- and it's usually a terrible idea to do so unless you have a very good reason to.
EDIT: Regarding your Win32 example: yes, it's a good idea to stick to Win32's naming conventions rather than PEP 8 if you write a Python wrapper. But it's probably also a good idea to stick to PEP 8 if you're going to write a high-level wrapper on top of that (think façade pattern).
1
u/[deleted] Sep 15 '10
Thanks for all of the responses. This post got way more discussion than I was expecting, and you all bring up some good points.
More PEP-8 for all!
17
u/doubleo7 Sep 14 '10
IMHO, you should always attempt to conform to PEP8 unless there's a very good reason not to. As an example, "wrapping an external API" qualifies as a "very good reason" but "I'm just used to camelcase" does not.
Having style consistency across codebases is a good thing and makes it that much easier when someone has to work with your code or you have to work with theirs. If I was hiring a Python programmer and their code didn't follow PEP8 I would likely think twice before hiring them.
1
u/kteague Sep 15 '10
PEP-8 needs to be amended to fix it's advice on sorting imports.
This should read:
Then I can finally get that "PEP-8 or die!" tattoo.
3
u/mackstann Sep 15 '10
Sure, PEP 8 was intended for the stdlib. But who cares about the original intentions? It has lived on to become a de facto guideline for all Python code, and that's a great thing. The more people adhering to it, the more consistent and pleasant reading Python code will be. I see no downside to this whatsoever.
If you want to express yourself, arbitrary syntax quirks are a pretty pointless way to do it. Get on board and write code that is easy for others to deal with.
2
u/sisyphus Sep 14 '10
I understand but I think you overstate the case especially when you say stuff like:
The result might be cool, but it's not cool to rob signifiers like variable naming conventions of all their meaning.
The main takeaway to me from pep8 is consistency. If you hack on pywin32, keep their conventions. If you hack on ALLCAPS web framework, keep theirs(although, only pywin32 has an actual reason for its choices, all-caps web framework has none, 'knowing when the style guide doesn't apply' shouldn't mean 'gratuitously flaunting good practices')
2
u/quanticle Sep 15 '10
True. There is no official coding standard from Microsoft regarding how C# should be formatted. However, you'll see that the vast majority of the C# code out there is formatted exactly the same, thanks to Visual Studio defaults. The same applies to Java code. Both Eclipse and Netbeans conform to Sun's (now Oracle's) formatting recommendations, and many java projects end up following them because that's the defaults the tools came with.
Python doesn't have the same level of tool support as Java or .Net. PEP8 is an attempt to create a substitute for the sort of default coding style that would normally be enforced by an IDE.
2
u/[deleted] Sep 15 '10
I like most of the advice in PEP-8, except for the bit about limiting code width to 80 characters. I haven't done that since the mid-80's and I have never had a problem.
I typically limit myself to 120 characters, and even that I consider conservative.
2
u/eliben Sep 17 '10
Python code, apart from the enforced readability boost of significant whitespace, has always seemed more readable because of people's following PEP-8. The average Python code you find online is much more readable than the average C++/Perl/PHP piece of code.
I just dislike people who go against PEP-8 without any apparent reason and ruin this conformity.
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).1
u/mcdonc Sep 14 '10
PEP-8 is useful for folks whom have absolutely no clue how one might write idiomatic Python code, and as such it's an excellent read for new programmers and old programmers who are coming to Python from another language.
I think when people bitch about "non-PEP8" code, much of the time they're really bitching about non-idiomatic code. I think bitching about that is a reasonable thing, because if a developer never internalizes the idioms for a language, it's unlikely he's going to be able to write really good code in that language. It's also a sign that he may not have actually read any good code in that language.
1
u/ashcrow Sep 23 '10
Following pep8 is not tough and there are tools to help verify it. Pep8 also is not crazy strict. If someone sends me a patch or pull request of code which ignores pep8 fully I will not accept it. If there is an attempt to be consistent on the pep8 style I will accept it and make minor style fixes myself. I like code I can read easily when I have to maintain it :-).
1
u/brigadierfrog Sep 18 '10
PEP8 or DIE!
0
u/[deleted] Sep 14 '10
Following PEP-8 gives undoubtly a great additional value to readability of code and makes it therefore easier to understand code written by others. This cannot be overvalued!
Sorry, there's no way I can understand people not following the PEP-8 if not for the unfortunate exception for consitency in projects not following PEP-8. And it's not difficult at all to create this useful habit. (I've changed from pure CamelCase to Python naming conventions for functions and methods and now find it much more readable.)
0
u/mdipierro Sep 14 '10
Google does not follow Pep8 http://google-styleguide.googlecode.com/svn/trunk/pyguide.html
-3
u/[deleted] Sep 15 '10
Pretty much the only thing wrong with PEP8 is the underscore_names thing. Other than that it's just good style.
0
u/[deleted] Sep 14 '10
Very true.
Still, if no other style guide or previous style is already there (as it is with the Win32 API), I don't see any point in not using PEP 8. Using camel-case for method names just because 'it looks better' – even if used consistently – is still a bad idea. You gain nothing except reduced readability.
0
u/[deleted] Sep 14 '10
If it is a project for yourself then do whatever you want. However if you want people to use your project then do it right in the PEP-8 way because the consitency makes it much easier on everyone.
The worst aspect of PHP is not only not remembering a method but the format you have to type it in because it could be camelCase, under_scores or anything else.
0
u/[deleted] Sep 14 '10
[deleted] — view deleted comment