Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: Re: Multiple inheritance, super() and changing signature Date: Sat, 04 Jun 2016 10:44:09 +1000 Lines: 56 Message-ID: References: <80ea0ea1-5468-2dee-2c38-c2b09801d0f1@shopzeus.com> <574e45f4$0$1611$c3e8da3$5496439d@news.astraweb.com> <85k2i65upp.fsf@benfinney.id.au> <85fusu58pw.fsf@benfinney.id.au> <85wpm54wbq.fsf@benfinney.id.au> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de zuAv7ulY01izRC2wIbDfBADjSPXkITNw1fvtxtaDeKMA== Cancel-Lock: sha1:NR/fEVtNW9AnnlePzFCmVIjdiN8= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; ':-(': 0.07; 'positional': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'semantics': 0.09; 'sucks': 0.09; 'python': 0.10; 'def': 0.13; 'argument': 0.15; '8bit%:26': 0.16; 'argument.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'sign.': 0.16; 'subject:changing': 0.16; 'advocate': 0.22; 'arguments': 0.22; 'code.': 0.23; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'handling': 0.27; 'parameters': 0.27; 'right.': 0.27; 'said,': 0.27; 'function': 0.28; 'actual': 0.28; '**kwargs):': 0.29; 'spam,': 0.29; 'wright': 0.29; 'allows': 0.30; "we're": 0.30; 'class.': 0.30; 'guess': 0.31; 'certain': 0.31; 'another': 0.32; 'skip:_ 10': 0.32; 'though,': 0.32; 'class': 0.33; 'except': 0.34; 'skip:d 20': 0.34; 'running': 0.34; 'next': 0.35; 'unknown': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'method': 0.37; 'received:org': 0.37; 'signature': 0.37; 'why': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'matter': 0.63; 'believe': 0.66; '"spam"': 0.84; '_o__)': 0.84; 'cop': 0.84; 'received:125': 0.84; 'inheritance,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <85wpm54wbq.fsf@benfinney.id.au> X-Mailman-Original-References: <80ea0ea1-5468-2dee-2c38-c2b09801d0f1@shopzeus.com> <574e45f4$0$1611$c3e8da3$5496439d@news.astraweb.com> <85k2i65upp.fsf@benfinney.id.au> <85fusu58pw.fsf@benfinney.id.au> Xref: csiph.com comp.lang.python:109447 Ian Kelly writes: > Except that since we're discussing design for multiple inheritance, > the positional argument "spam" is inappropriate. All arguments should > be passed by keyword; the DolorSitAmet.__init__ method cannot be > certain that LoremIpsum will be the next class in the MRO, and the > actual next class might not expect spam to be the first positional > argument. You're right. That also allows us to stop handling unknown positional arguments. This does make it troublesome to design the function signature though, and I can see why people balk at how to deal with the semantics of ‘super’ in Python 2:: class LoremIpsum(object): def __init__(self, **kwargs): spam = kwargs.pop('spam') do_something_important_with(spam) super(LoremIpsum, self).__init__(**kwargs) class DolorSitAmet(LoremIpsum): def __init__(self, **kwargs): self.eggs = kwargs.pop('eggs') self.beans = kwargs.pop('beans') super(DolorSitAmet, self).__init__(**kwargs) That's awful :-( because the initialiser's signature no longer shows any sign of which parameters matter for this class. It also sucks to need ‘dict.pop('name')’, instead of just ‘name’. Keyword-only parameters make this easier and clearer:: class LoremIpsum: def __init__(self, *, spam, **kwargs): spam = kwargs.pop('spam') do_something_important_with(spam) super().__init__(**kwargs) class DolorSitAmet(LoremIpsum): def __init__(self, *, eggs=4, beans=None, **kwargs): self.eggs = eggs self.beans = beans super().__init__(**kwargs) I guess that's yet another reason to advocate Python 3 for all new code. -- \ “One time a cop pulled me over for running a stop sign. He | `\ said, ‘Didn't you see the stop sign?’ I said, ‘Yeah, but I | _o__) don't believe everything I read.’” —Steven Wright | Ben Finney