Closed jni closed 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.
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.
@jni Thanks again for the feedback. I made the note much clearer, I think, in commit 944e71a.
@amontalenti :+1:! Thanks!
On prescribing "new-style classes", you write,
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".