Pergunta

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?

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top