Pushpak Dagade
Pushpak Dagade

Reputation: 6460

Why is a good practice to inherit any class we make from the class 'object'?

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?

Upvotes: 3

Views: 229

Answers (2)

unutbu
unutbu

Reputation: 880667

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.

Upvotes: 7

virhilo
virhilo

Reputation: 6793

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

Upvotes: 0

Related Questions