8

In one of my past questions, a answerer suggests me that it is better to inherit from object when the class you want to create is like from scratch, which is no need to inherit from other class.

For example, like what I always do:

class my_class:
     "a class inherits from nothing"
     def __init__(self):
         pass

For what he or she suggested:

class suggested_class(object):
     "a class inherits from object type"
     def __init__(self):
         pass

I am confused with the benefits or disadvantage from both approaches.

Question 1:

So what is your idea, inherit from object type or nothing?

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
xiaohan2012
  • 9,870
  • 23
  • 67
  • 101
  • 2
    see http://docs.python.org/release/2.2.3/whatsnew/sect-rellinks.html#SECTION000310000000000000000 – wim Sep 05 '11 at 05:15
  • As Marcello mentions, if you have two questions, please limit this post to a single question and [post another question](http://stackoverflow.com/questions/ask) for the other. – SingleNegationElimination Sep 05 '11 at 05:35
  • You can have a look at `dir(my_class)` and `dir(suggested_class)`. Furthermore, as mentioned below, both will be the same in Python3. – steabert Sep 05 '11 at 06:57

1 Answers1

5

Inheriting from nothing creates an old-style class, which has different behaviour to new-style classes. I don't remember the specifics just now (see here for an explanation), but as a general rule, there's no reason to favour old-style classes, so you should always inherit from object (if nothing else).

Ferrybig
  • 18,194
  • 6
  • 57
  • 79
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • 4
    The reason for having new style classes is 1: the types of all instances of old style classes was `classobj`, which isn't very interesting, and 2: method resolution order on old style classes was broken for multiple inheritance. Fixing one or both of these issues would have made a lot of existing code stop working, so the simplest thing to do was make a different kind of class, thus `object`. – SingleNegationElimination Sep 05 '11 at 05:34
  • 2
    @Token Also, in Python 3, all classes are back to being new-style without specifically inheriting from `object`. – Keith Sep 05 '11 at 05:36
  • Well, old style classes were broken, and py3 intentionally breaks backward compatability, so that's a great time to make the jump. – SingleNegationElimination Sep 05 '11 at 05:37
  • @TokenMacGuy, Can you give more hint on why method resolution order on old style classes was broken for multiple inheritance? it seems unclear to me. – xiaohan2012 Sep 05 '11 at 06:08
  • The short answer is that the old mro didn't preserve the order superclasses were listed in, So subclasses couldn't be sure what order the methods would be called in; for more details on the matter, read [The Python 2.3 Method Resolution Order](http://www.python.org/download/releases/2.3/mro/) – SingleNegationElimination Sep 05 '11 at 06:19
  • Perhaps more practically, properties don't work on old-style classes. – Fred Foo Sep 19 '12 at 09:40