Should all Python classes extend object? [duplicate]
Solution 1:
In Python 2, not inheriting from object
will create an old-style class, which, amongst other effects, causes type
to give different results:
>>> class Foo: pass
...
>>> type(Foo())
<type 'instance'>
vs.
>>> class Bar(object): pass
...
>>> type(Bar())
<class '__main__.Bar'>
Also the rules for multiple inheritance are different in ways that I won't even try to summarize here. All good documentation that I've seen about MI describes new-style classes.
Finally, old-style classes have disappeared in Python 3, and inheritance from object
has become implicit. So, always prefer new style classes unless you need backward compat with old software.
Solution 2:
In Python 3, classes extend object
implicitly, whether you say so yourself or not.
In Python 2, there's old-style and new-style classes. To signal a class is new-style, you have to inherit explicitly from object
. If not, the old-style implementation is used.
You generally want a new-style class. Inherit from object
explicitly. Note that this also applies to Python 3 code that aims to be compatible with Python 2.