Quiz: Difference between implicit and explicit inheritence

Steve D'Aprano steve+python at pearwood.info
Mon Aug 28 21:24:10 EDT 2017


On Mon, 28 Aug 2017 11:27 pm, Pavol Lisy wrote:

> object = int  # this could be trick
> ...
> class Spam(object):
>     ...
> 
> dis.dis show difference too, next line is just for "class
> Spam(object):" version:
>      LOAD_GLOBAL              0 (object)
> 
> So nor rebinding object nor rebinding __builtin__.object affect "class
> Spam:" version.

Well done! That's exactly right.

When you explicitly inherit from object, Python does a normal name lookup to
determine what object is, and so if you have shadowed or rebound the name, you
will inherit from whatever object happens to resolve to.

When you implicitly inherit from object, the Python interpreter automatically
uses the true builtin object as base class, even if it has been shadowed or
rebound.

See http://bugs.python.org/issue31283 



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list