[Python-Dev] class Foo(object) vs class Foo: should be clearly explained in python 2 and 3 doc

Steven D'Aprano steve at pearwood.info
Sun Aug 10 02:44:52 CEST 2014


On Sat, Aug 09, 2014 at 02:44:10PM -0400, John Yeuk Hon Wong wrote:
> Hi.
> 
> Referring to my discussion on [1] and then on #python this afternoon.
> 
> A little background would help people to understand where this was 
> coming from.
> 
> 1. I write Python 2 code and have done zero Python-3 specific code.
> 2. I have always been using class Foo(object) so I do not know the new 
> style is no longer required in Python 3. I feel "stupid" and "wrong" by 
> thinking (object) is still a convention in Python 3.

But object is still a convention in Python 3.

It is certainly required when writing code that will behave the same in 
version 2 and 3, and it's optional in 3-only code, but certainly not 
frowned upon or discouraged. There's nothing wrong with explicitly 
inheriting from object in Python 3, and with the Zen of Python "Explicit 
is better than implicit" I would argue that *leaving it out* should be 
very slightly discouraged.

class Spam:  # okay, but a bit lazy
class Spam(object):  # better

Perhaps PEP 8 should make a recommendation, but if so, I think it should 
be a very weak one. In Python 3, it really doesn't matter which you 
write. My own personal practice is to explicitly inherit from object 
when the class is "important" or more than half a dozen lines, and leave 
it out if the class is a stub or tiny.


> 3. Many Python 2 tutorials do not use object as the base class whether 
> for historical reason, or lack of information/education, and can cause 
> confusing to newcomers searching for answers when they consult the 
> official documentation.

We can't do anything about third party tutorials :-(


> While Python 3 code no longer requires object be the base class for the 
> new-style class definition, I believe (object) is still required if one 
> has to write a 2-3 compatible code. But this was not explained or warned 
> anywhere in Python 2 and Python 3 code, AFAIK. (if I am wrong, please 
> correct me)

It's not *always* required, only if you use features which require 
new-style classes, e.g. super, or properties.


> I propose the followings:
> 
> * It is desirable to state boldly to users that (object) is no longer 
> needed in Python-3 **only** code 

I'm against that. Stating this boldly will be understood by some readers 
that object should not be used, and I'm strongly against that. I believe 
explicitly inheriting from object should be mildly preferred, not 
strongly discouraged.


> and warn users to revert to (object) 
> style if the code needs to be 2 and 3 compatible.

I don't think that should be necesary, but have no objections to it 
being mentioned. I think it should be obvious: if you need new-style 
behaviour in Python 2, then obviously you have to inherit from object 
otherwise you have a classic class. That requirement doesn't go away 
just because your code will sometimes run under Python 3.


Looking at your comment here:

> [1]: https://news.ycombinator.com/item?id=8154471

there is a reply from zeckalpha, who says:

   "Actually, leaving out `object` is the preferred convention for 
    Python 3, as they are semantically equivalent."

How does (s)he justify this claim?

   "Explicit is better than implicit."

which is not logical. If you leave out `object`, that's implicit, not 
explicit.


-- 
Steven


More information about the Python-Dev mailing list