Mi rendo conto che questa è una vecchia domanda, ma vale la pena notare che anche in Python 3 queste due cose non sono esattamente la stessa cosa.
Se si eredita esplicitamente da object
, ciò che si sta effettivamente facendo eredita da builtins.object
indipendentemente da ciò a cui punta in quel momento.
Pertanto, potrei avere un modulo (molto stravagante) che sovrascrive l'oggetto per qualche motivo. Chiameremo questo primo modulo "newobj.py":
import builtins
old_object = builtins.object # otherwise cyclic dependencies
class new_object(old_object):
def __init__(self, *args, **kwargs):
super(new_object, self).__init__(*args, **kwargs)
self.greeting = "Hello World!"
builtins.object = new_object #overrides the default object
Poi in qualche altro file ("klasses.py"):
class Greeter(object):
pass
class NonGreeter:
pass
Poi, in un terzo file (che si può effettivamente gestita):
import newobj, klasses # This order matters!
greeter = klasses.Greeter()
print(greeter.greeting) # prints the greeting in the new __init__
non_greeter = klasses.NonGreeter()
print(non_greeter.greeting) # throws an attribute error
Così si può vedere che, nel caso in cui è esplicitamente che eredita da oggetto, si ottiene un comportamento diverso da quello in cui si consente l'eredità implicita.
fonte
2017-08-26 09:03:10
Se il codice verrà utilizzato anche in 2.x, è opportuno essere espliciti. – smci