What is the semantics meaning of 'object'?

Roy Smith roy at panix.com
Sun Jun 23 14:14:16 EDT 2013


In article <mailman.3730.1372007386.3114.python-list at python.org>,
 Ian Kelly <ian.g.kelly at gmail.com> wrote:

> Yes, you're missing that super() does not simply call the base class,
> but rather the next class in the MRO for whatever the type of the
> "self" argument is.  If you write the above as:
> 
> class Base1(object):
>    def __init__(self, foo, **kwargs):
>       super(Base1, self).__init__(**kwargs)
> 
> class Base2(object):
>    def __init__(self, bar, **kwargs):
>       super(Base2, self).__init__(**kwargs)
> 
> class Derived(Base1, Base2):
>    def __init__(self, **kwargs):
>       super(Derived, self).__init__(**kwargs)
> 
> And then you create an instance of Derived by calling
> Derived(foo='foo', bar='bar') and trace the call chain, you find that
> Derived.__init__ will call Base1.__init__(foo='foo', bar='bar'), which
> extracts its argument and then calls (surprise!)
> Base2.__init__(bar='bar'), which again extracts its argument and then
> calls object.__init__(), ending the chain.

Mind.  Blown.

I'm tempted to go all Ranting Rick about this being non-obvious, but I 
see you already covered that in another post :-)

The other thing I see here is that to make this work, the base classes 
need to accept **kwargs.  That's kind of annoying, since it means a 
class has to explicitly designed to be multiply-inheritable.



More information about the Python-list mailing list