Issue
While reviewing some code, it has a structure like
class Bar(Foo, object):
Which seems like it could easily be written instead as
class Bar(Foo):
The functionally appears at least the same the simple chain of inheritance for the purpose of determining method resolution, and both Foo
and Bar
are isinstance()
of object
in both cases
B → F → o
B → F
↳ o
Is there any practical benefit to a multiple inheritance from object
?
Solution
Almost certainly, no, it shouldn't have any effect, the MRO is still the same:
>>> class Foo: pass
...
>>> class Bar(Foo, object): pass
...
>>> Bar.mro()
[<class '__main__.Bar'>, <class '__main__.Foo'>, <class 'object'>]
So:
>>> class Bar(Foo): pass
...
>>> Bar.mro()
[<class '__main__.Bar'>, <class '__main__.Foo'>, <class 'object'>]
Potentially, I suppose, it could have an effect if any code introspects __bases__
:
>>> class Bar(Foo): pass
...
>>> Bar.__bases__
(<class '__main__.Foo'>,)
>>> class Bar(Foo, object): pass
...
>>> Bar.__bases__
(<class '__main__.Foo'>, <class 'object'>)
But I highly suspect this is just a typo
Answered By - juanpa.arrivillaga
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.