amontalenti / elements-of-python-style

Goes beyond PEP8 to discuss what makes Python code feel great. A Strunk & White for Python.
3.44k stars 260 forks source link

Make Python 3 the first-class citizen #17

Closed jni closed 8 years ago

jni commented 8 years ago

On prescribing "new-style classes", you write,

(This rule flips in Python 3.)

First point: can you elaborate on that? I actually hadn't realised this change, even though I'm a big Python 3 advocate. Do you have a reference for this?

Second point: given that this is a document on best practices, and we should be encouraging readers to use Python 3, I would argue that it makes more sense to write "DON'T inherit from object [...](This rule flips in Python 2.)" Or, at least, put the qualifier in the title: "When writing Python 2, use new-style classes".

amontalenti commented 8 years ago

I guess it was only a matter of time before we started getting into Python 2 vs 3 issues :grin:

So, in Python 2 code, people should always inherit from object. This is because doing so is what turns a class into a "new-style class" (which is what is discussed in the link from the section).

In Python 3 code, old-style classes are gone, or, put another way, every class is the same. Inheriting from object in Python 3 is just needless typing -- it's a no-op. Thus in mixed source trees (that are meant to run in Python 2 and 3), you should still inherit from object. It's not ideal, but I think the rule, as I have it written, is still the best advice.

So much of the advice in this style guide applies to Python 2 and Python 3, and I'd prefer to keep it that way. The language did not change that dramatically in Python 3, and all the new Python 3 idioms are covered well elsewhere.

jni commented 8 years ago

Oh, sure, I agree that almost all the advice applies to both. Since (object) is a no-op in Python 3, I would then suggest simply removing the "this rule flips" comment: it's not exactly right, and would be confusing to newcomers.

amontalenti commented 8 years ago

@jni Thanks again for the feedback. I made the note much clearer, I think, in commit 944e71a.

jni commented 8 years ago

@amontalenti :+1:! Thanks!