3

In python, why is recommended to inherit any class we make from the class object , why not directly make it as the base class?? An important thing I noticed is that the declaration __slots__ does not work if I make my class as a base class (instead as a subclass of the class object). What other advantages/disadvantages do I have by inheriting my class from the class object?

Karl Knechtel
  • 56,349
  • 8
  • 83
  • 124
Pushpak Dagade
  • 5,926
  • 7
  • 26
  • 39
  • This is not quite a duplicate, but please refer to http://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python . – Karl Knechtel Jan 08 '11 at 15:53
  • possible duplicate of [python class inherits object](http://stackoverflow.com/questions/4015417/python-class-inherits-object) –  Jan 08 '11 at 16:59
  • you can find this easily by searching for "python inherit from object" –  Jan 08 '11 at 17:00

2 Answers2

7

In Python2, you must inherit from object in order to create a "new-style" class. Things like descriptors, super and __slots__ do not work correctly with "old-style" classes, but old-style classes remained for backwards compatibility.

In Python3, all classes are new-style classes, so inheriting from object is no longer necessary.

unutbu
  • 777,569
  • 165
  • 1,697
  • 1,613
0

when you inheriting from the object you create new style class, without it you have old style class see: http://www.python.org/doc/newstyle/ for more

virhilo
  • 6,026
  • 1
  • 27
  • 26