Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why inheriting from object type

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?

like image 392
xiaohan2012 Avatar asked Sep 05 '11 05:09

xiaohan2012


1 Answers

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).

like image 83
Marcelo Cantos Avatar answered Sep 22 '22 13:09

Marcelo Cantos