Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #109414

Re: Multiple inheritance, super() and changing signature

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Nagy László Zsolt <gandalf@shopzeus.com>
Newsgroups comp.lang.python
Subject Re: Multiple inheritance, super() and changing signature
Date Fri, 3 Jun 2016 16:06:12 +0200
Lines 85
Message-ID <mailman.125.1464962774.1839.python-list@python.org> (permalink)
References <80ea0ea1-5468-2dee-2c38-c2b09801d0f1@shopzeus.com> <mailman.54.1464711627.1839.python-list@python.org> <574e45f4$0$1611$c3e8da3$5496439d@news.astraweb.com> <a17955cb-04d3-9dd6-e2b3-4719269f241d@shopzeus.com> <d91f49c5-4572-9cc4-9688-b4b3ffbce691@shopzeus.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de 4MBFCGUQcBK38ZBMkNnUGwD/mxI1Vi/2eOd+Xmj6UF5Q==
Return-Path <gandalf@shopzeus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.058
X-Spam-Evidence '*H*': 0.89; '*S*': 0.00; 'modified': 0.05; 'classes.': 0.07; '__init__': 0.09; 'argument:': 0.09; 'positional': 0.09; 'typeerror:': 0.09; 'def': 0.13; 'received:io': 0.16; 'received:psf.io': 0.16; 'result:': 0.16; 'root:': 0.16; 'subject:changing': 0.16; 'sure.': 0.16; 'pass': 0.22; 'this:': 0.23; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'signatures': 0.29; 'classes': 0.30; 'code': 0.30; 'becomes': 0.30; 'raymond': 0.30; 'skip:_ 10': 0.32; 'class': 0.33; 'traceback': 0.33; 'changing': 0.34; 'file': 0.34; 'could': 0.35; 'skip:s 60': 0.35; 'something': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'missing': 0.37; 'self': 0.38; 'to:addr:python.org': 0.40; 'still': 0.40; 'some': 0.40; 'easy': 0.60; 'share': 0.61; 'watch': 0.62; 'charset:windows-1252': 0.62; 'url:png': 0.64; '20,': 0.66; 'url:26': 0.66; 'here': 0.66; 'url:2011': 0.75; 'url:i': 0.77; 'completion': 0.79; 'url:wordpress': 0.79; 'url:imgur': 0.84; 'shows,': 0.91; 'cooperative': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=shopzeus.com; s=shopzeus_com; t=1464962772; bh=IUDfiekzisFQptedAJEmd8sM2oAdSiPGkxfk8J99TJI=; h=Subject:To:References:From:Date:In-Reply-To:From; b=eS5zbNZzEDKd7Gbc4ZWHa0Z8HCyOHZ/Ka8HLatV1/zjL76Yt2sFiiKDgP+5NG6sV4 4afHb7IxUAvPKBmTqdVp2DrXyQHejbHXjuC1CBEbNF/19gC2/oxTdknUrY3CLscCYI BOMRkCdsf8J2DQR5K4yDrzRnX8Jw+jitTDD+nk5belCaMVHZK4D0tqBUAmQJ6wFo7M P3B/Mxhvx26mVgmOSb3ynBglkAaa6DB07MkgTYFQIDpVDyneLmrTmul1v/JQyTJyTi InaHXL4vnhtavPaY3POnsIAvgc3yHQ0yri5QxWzFp1zwCvXZIiCvi+xHLCshuPlNxS +VpuAHLV/yKgA==
In-Reply-To <a17955cb-04d3-9dd6-e2b3-4719269f241d@shopzeus.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <d91f49c5-4572-9cc4-9688-b4b3ffbce691@shopzeus.com>
X-Mailman-Original-References <80ea0ea1-5468-2dee-2c38-c2b09801d0f1@shopzeus.com> <mailman.54.1464711627.1839.python-list@python.org> <574e45f4$0$1611$c3e8da3$5496439d@news.astraweb.com> <a17955cb-04d3-9dd6-e2b3-4719269f241d@shopzeus.com>
Xref csiph.com comp.lang.python:109414

Show key headers only | View raw


>> That's overly strict. As Raymond shows, it is easy to deal with
>> changing method signatures in *cooperative* classes. 
> I must watch that for sure.

All right, I have read this:

https://rhettinger.wordpress.com/2011/05/26/super-considered-super/

There is still something I don't get: how to create cooperative classes
when some base classes share some of the parameters?

Here is an example modified from Raymond's post:

class A:
    def __init__(self, param1, param2, **kwds):
        self.param1 = param1
        self.param2 = param2
        super().__init__(**kwds)

class B:
    def __init__(self, param1, param3, **kwds):
        self.param1 = param1
        self.param3 = param3
        super().__init__(**kwds)


class X(A,B):
    def __init__(self, param1, param2, param3, **kwds):
        print("param1=",param1,"param2=",param2,"param3=",param3)
        super().__init__(param1=param1,param2=param2,param3=param3,**kwds)

print(X.__mro__)
x = X(1,2,3)


Result:

(<class '__main__.X'>, <class '__main__.A'>, <class '__main__.B'>,
<class 'object'>)
param1= 1 param2= 2 param3= 3
Traceback (most recent call last):
  File "test.py", line 20, in <module>
    x = X(1,2,3)
  File "test.py", line 17, in __init__
    super().__init__(param1=param1,param2=param2,param3=param3,**kwds)
  File "test.py", line 5, in __init__
    super().__init__(**kwds)
TypeError: __init__() missing 1 required positional argument: 'param1'

I could only find this as a solution:

class Root:
    def __init__(self, **kwds):
        pass

class A(Root):
    def __init__(self, **kwds):
        self.param1 = kwds['param1']
        self.param2 = kwds['param2']
        super().__init__(**kwds)

class B(Root):
    def __init__(self, **kwds):
        self.param1 = kwds['param1']
        self.param3 = kwds['param3']
        super().__init__(**kwds)


class X(A,B):
    def __init__(self, param1, param2, param3, **kwds):
        print("param1=",param1,"param2=",param2,"param3=",param3)
        super().__init__(param1=param1,param2=param2,param3=param3,**kwds)

X(1,2,3)

But then self documentation and code completion becomes very problematic:

http://i.imgur.com/wzlh8uy.png

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Multiple inheritance, super() and changing signature Nagy László Zsolt <gandalf@shopzeus.com> - 2016-05-31 18:10 +0200
  Re: Multiple inheritance, super() and changing signature Steven D'Aprano <steve@pearwood.info> - 2016-06-01 12:18 +1000
    Re: Multiple inheritance, super() and changing signature Nagy László Zsolt <gandalf@shopzeus.com> - 2016-06-03 14:10 +0200
      Re: Multiple inheritance, super() and changing signature Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-06-04 13:06 +1200
        Re: Multiple inheritance, super() and changing signature Steven D'Aprano <steve@pearwood.info> - 2016-06-04 12:51 +1000
          Re: Multiple inheritance, super() and changing signature Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-06-04 23:50 +1200
        Re: Multiple inheritance, super() and changing signature Ian Kelly <ian.g.kelly@gmail.com> - 2016-06-03 23:05 -0600
          Re: Multiple inheritance, super() and changing signature Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-06-04 23:52 +1200
            Re: Multiple inheritance, super() and changing signature Steven D'Aprano <steve@pearwood.info> - 2016-06-04 23:46 +1000
              Re: Multiple inheritance, super() and changing signature Nagy László Zsolt <gandalf@shopzeus.com> - 2016-06-04 16:42 +0200
    Re: Multiple inheritance, super() and changing signature Ben Finney <ben+python@benfinney.id.au> - 2016-06-03 22:21 +1000
      Re: Multiple inheritance, super() and changing signature Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-06-04 13:10 +1200
    Re: Multiple inheritance, super() and changing signature Nagy László Zsolt <gandalf@shopzeus.com> - 2016-06-03 14:14 +0200
    Re: Multiple inheritance, super() and changing signature Nagy László Zsolt <gandalf@shopzeus.com> - 2016-06-03 14:57 +0200
    Re: Multiple inheritance, super() and changing signature Nagy László Zsolt <gandalf@shopzeus.com> - 2016-06-03 16:06 +0200
    Re: Multiple inheritance, super() and changing signature Ian Kelly <ian.g.kelly@gmail.com> - 2016-06-03 08:39 -0600
    Re: Multiple inheritance, super() and changing signature Michael Selik <michael.selik@gmail.com> - 2016-06-03 15:33 +0000
    Re: Multiple inheritance, super() and changing signature Nagy László Zsolt <gandalf@shopzeus.com> - 2016-06-03 17:59 +0200
    Re: Multiple inheritance, super() and changing signature Michael Selik <michael.selik@gmail.com> - 2016-06-03 16:28 +0000
    Re: Multiple inheritance, super() and changing signature Ben Finney <ben+python@benfinney.id.au> - 2016-06-04 06:16 +1000
    Re: Multiple inheritance, super() and changing signature Ben Finney <ben+python@benfinney.id.au> - 2016-06-04 06:19 +1000
    Re: Multiple inheritance, super() and changing signature Ian Kelly <ian.g.kelly@gmail.com> - 2016-06-03 15:53 -0600
    Re: Multiple inheritance, super() and changing signature Ben Finney <ben+python@benfinney.id.au> - 2016-06-04 10:44 +1000

csiph-web