perché eredita da tipo di oggetto
-
25-10-2019 - |
Domanda
In una delle mie domande precedenti, un risposto mi suggerisce che è meglio ereditare dall'oggetto quando la classe che si desidera creare è come dal nulla, che c'è bisogno di ereditare da un'altra classe.
Per esempio, come quello che faccio sempre:
class my_class:
"a class inherits from nothing"
def __init__(self):
pass
Per quello che lui o lei ha suggerito:
class suggested_class(object):
"a class inherits from object type"
def __init__(self):
pass
Sono confuso con i vantaggi o svantaggi di entrambi gli approcci.
Domanda 1:
Allora, qual è la tua idea, eredita dal tipo di oggetto o niente?
Soluzione
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).