Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'argument': 0.05; 'class,': 0.07; 'classes.': 0.09; 'derived': 0.09; 'here?': 0.09; 'python': 0.11; 'def': 0.12; 'assume': 0.14; '**kwargs):': 0.16; '10:49': 0.16; '23,': 0.16; 'foo,': 0.16; 'inheritance': 0.16; 'mro': 0.16; 'roy': 0.16; 'subject:object': 0.16; 'wrote:': 0.18; "i've": 0.25; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'appear': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'directly,': 0.31; 'occurs': 0.31; 'trace': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'subject:the': 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'next': 0.36; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'is.': 0.60; 'course': 0.61; 'simply': 0.61; "you're": 0.61; 'smith': 0.68; 'ending': 0.78; 'as:': 0.81; 'bar)': 0.84; 'do:': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=X4MwS3WaPffq7CSo1bMswNDRpPH8gOrwj3TnrI3heSs=; b=yjRymhxG/KsbcyEhqUKkQc9KgEZOcpWdVZynEOz4opDJSlMML8uOgpGRFO5H5vaTGv QGM/9fOlHO+J5SkYzmGJWly5Y5qC6bo9JZZwLmyKefXXuYzEAQy4N6jTL4rp9jEdGb63 DKbPp6Ut4v2HbAvyZ1wTu4oSDCihZK1+kdFqsNhADswjwxncqlTPzaO/saeLUzOKbqw0 xiEVLxqhY0FzVJRjos2WGcYJaC7qa42afRp5XGzf1+fVoRtv7Hl5SPgBkcaLaPtve/1w SbiyjhbRwv5oQqJxu4gBUTOkgG7Qk0evJzXhkWjSGI/HPXz16GXnq5V9M8nCDm76xOOI wg7g== X-Received: by 10.68.42.134 with SMTP id o6mr5828753pbl.149.1372007376962; Sun, 23 Jun 2013 10:09:36 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <15ba0011-bbf1-42f7-b3ea-1c1d4b70e56b@googlegroups.com> <51c66962$0$29999$c3e8da3$5496439d@news.astraweb.com> <20130623133546.GA2308@capricorn> <51c723b4$0$29999$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Sun, 23 Jun 2013 11:08:56 -0600 Subject: Re: What is the semantics meaning of 'object'? To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372007386 news.xs4all.nl 15879 [2001:888:2000:d::a6]:52921 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:48992 On Sun, Jun 23, 2013 at 10:49 AM, Roy Smith wrote: > One thing I've never understood about Python 2.x's multiple inheritance > (mostly because I almost never use it) is how you do something like this: > > class Base1(object): > def __init__(self, foo): > self.foo = foo > > class Base2(object): > def __init__(self, bar): > self.bar = bar > > class Derived(Base1, Base2): > def __init__(self, foo, bar): > # now what??? > > I need to call __init__() in both of my base classes. I don't see how > super() can do that for me. I assume I would just do: > > def __init__(self, foo, bar): > Base1.__init__(self, foo) > Base2.__init__(self, bar) > > am I missing something here? 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. Of course if you create an instance of Base1, then the Base1 super call will next call object.__init__ directly, instead of Base2.__init__. This happens because Base2 occurs after Base1 in the MRO for the class Derived, but Base2 does not appear at all in the MRO for the class Base1.