108

In older Python versions when you create a class, it can inherit from object which is as far I understand a special built-in Python element that allows your class to be a new-style class.

What about newer versions (> 3.0 and 2.6)? I googled about the object class but I get so many results (for obvious reasons).

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
thomas
  • 1,855
  • 4
  • 17
  • 19

2 Answers2

118

You don't need to inherit from object to have new style in python 3. All classes are new-style.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • 9
    Notice that this means all classes inherit from `object` regardless if typing an explicit `(object)` or not in Python 3.1 – u0b34a0f6ae Dec 04 '09 at 17:47
  • 9
    You don't have to, but the "Porting Python code to Python 3" says that it's still valid: http://docs.python.org/py3k/howto/pyporting.html#subclass-object Also: http://docs.python.org/reference/datamodel.html#new-style-and-classic-classes – hyperboreean Oct 17 '12 at 12:36
  • From [this post](https://stackoverflow.com/a/9448136/7829525) mentioned in nngeek's comment, seems like a good stable reference for old-style vs. new-style (only really relevant to Py2) is: https://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes - the above links seemed to have since changed. – Eric Cousineau Feb 26 '20 at 23:19
93

I realise that this is an old question, but it is worth noting that even in python 3 these two things are not quite the same thing.

If you explicitly inherit from object, what you are actually doing is inheriting from builtins.object regardless of what that points to at the time.

Therefore, I could have some (very wacky) module which overrides object for some reason. We'll call this first module "newobj.py":

import builtins

old_object = builtins.object  # otherwise cyclic dependencies

class new_object(old_object):

    def __init__(self, *args, **kwargs):
        super(new_object, self).__init__(*args, **kwargs)
        self.greeting = "Hello World!" 

builtins.object = new_object  #overrides the default object

Then in some other file ("klasses.py"):

class Greeter(object):
    pass

class NonGreeter:
    pass

Then in a third file (which we can actually run):

import newobj, klasses  # This order matters!

greeter = klasses.Greeter()
print(greeter.greeting)  # prints the greeting in the new __init__

non_greeter = klasses.NonGreeter()
print(non_greeter.greeting) # throws an attribute error

So you can see that, in the case where it is explicitly inheriting from object, we get a different behaviour than where you allow the implicit inheritance.

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
Philip Adler
  • 2,111
  • 17
  • 25
  • 19
    It's relevant because in general the expectance is that the behaviour is equivalent. It isn't equivalent, hence my observations. – Philip Adler Jan 23 '20 at 08:18
  • 12
    Don't you just hate it when someone deletes their comments so it looks like you're talking to yourself? – Philip Adler Jan 19 '22 at 15:28