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: Wed, 01 Jun 2016 06:01:43 +1000 Lines: 132 Message-ID: References: <80ea0ea1-5468-2dee-2c38-c2b09801d0f1@shopzeus.com> <85h9de6lp4.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 YP6GjEDD7cXnoKoe3m8NCAcWKsolWZRW8EUx0WmlK6ZQ== Cancel-Lock: sha1:hvBtHFHAyTOdnrkq6Hynqxo+1JE= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'anyway.': 0.04; 'method.': 0.05; 'bootstrap': 0.07; 'classes.': 0.07; 'constructor': 0.07; 'method,': 0.07; 'override': 0.07; 'remaining': 0.07; 'likewise': 0.09; 'obey': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'substitution': 0.09; 'example:': 0.10; 'times,': 0.13; 'def': 0.13; 'explicitly': 0.15; 'desktops': 0.16; 'hierarchy.': 0.16; 'inheritance': 0.16; 'parameters,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'stallman': 0.16; 'sub-class': 0.16; 'subject:changing': 0.16; 'example.': 0.18; 'instance,': 0.18; 'pointed': 0.18; 'typing': 0.18; 'widget': 0.18; '(not': 0.20; 'arguments': 0.22; 'parameter': 0.22; 'visible': 0.22; 'pass': 0.22; 'code.': 0.23; "python's": 0.23; 'skip:b 30': 0.24; 'somewhere': 0.24; 'all.': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'example': 0.26; 'appear': 0.26; 'header:X -Complaints-To:1': 0.26; 'error': 0.27; 'define': 0.27; 'parameters': 0.27; 'connects': 0.27; 'function': 0.28; 'resolution': 0.28; '**kwargs):': 0.29; 'arguments,': 0.29; 'be:': 0.29; 'behaviour': 0.29; 'signatures': 0.29; 'handled': 0.29; 'classes': 0.30; 'code': 0.30; 'raymond': 0.30; 'skip:_ 10': 0.32; 'implement': 0.32; 'class': 0.33; 'problem': 0.33; 'safely': 0.33; 'except': 0.34; 'could': 0.35; 'done': 0.35; 'unknown': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'skip:m 40': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'signature': 0.37; 'skip:v 20': 0.38; 'names': 0.38; 'anything': 0.38; 'thank': 0.38; 'means': 0.39; 'does': 0.39; 'along': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'avoid': 0.61; 'default': 0.61; 'more': 0.63; 'places': 0.64; 'today': 0.65; 'numerous': 0.66; 'url:26': 0.66; 'here': 0.66; 'results': 0.66; 'fact,': 0.67; 'skip:\xe2 10': 0.70; 'social': 0.71; '8bit%:40': 0.72; '8bit%:43': 0.72; 'url:2011': 0.75; 'protect': 0.76; 'article': 0.77; 'url:wordpress': 0.79; '_o__)': 0.84; 'anywhere.': 0.84; 'complexity': 0.84; 'here...': 0.84; 'idiom': 0.84; 'nagy': 0.84; 'received:125': 0.84; 'visually': 0.84; '\xe2\x80\xa6': 0.84; 'articles,': 0.91; 'tricky': 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: <85h9de6lp4.fsf@benfinney.id.au> X-Mailman-Original-References: <80ea0ea1-5468-2dee-2c38-c2b09801d0f1@shopzeus.com> Xref: csiph.com comp.lang.python:109291 Nagy László Zsolt writes: > Today I come across this problem for the N+1st time. Here are some > classes for the example: Thank you for the example. (Note that ‘__init__’ is not a constructor, because it operates on the *already constructed* instance, and does not return anything. Python's classes implement the constructor as ‘__new__’, and you very rarely need to bother with that.) For the reasons you describe (among others), participating in multiple inheritance is tricky. As described in numerous articles, for example , the right way to do this is: * Obey the Liskov Substitution Principle (sub-classes should override a method only when they allow all the parent's behaviour as well as their own). This also means that the sub-class should pass any unknown arguments along to the superclass's method to handle. * Once a parameter is handled in a method, remove it from the set and pass only the remaining arguments along to the superclass's method. > class Observable: > """Implements the observer-observable pattern.""" > > def __init__(self): > # initialization code here... > super(Observable, self).__init__() This should be: def __init__(self, **kwargs): # … super(Observable, self).__init__(**kwargs) > class Widget(Observable, AppServerSessionMixin): > def __init__(self, appserver, session): > self.appserver = appserver > self.session = session > # general widget initialization code here... > super(Widget, self).__init__() This should be: def __init__(self, appserver, session, **kwargs): self.appserver = appserver self.session = session # … super(Widget, self).__init__(**kwargs) > class VisualWidget(Widget): > def __init__(self, appserver, session): > # some more code here for visually visible widgets... > super(VisualWidget, self).__init__(appserver, session) Since this function doesn't do anything with the parameters, it doesn't need to have its own names for them: def __init__(self, **kwargs): # … super(VisualWidget, self).__init__(**kwargs) > class BaseDesktop: > def __init__(self, appserver, session): > # general code for all desktops is here... > super(BaseDesktop, self).__init__(appserver, session) > > class BootstrapWidget(VisualWidget): > def __init__(self, appserver, session): > # bootstrap specific code for all widgets (not just desktops) > super(BootstrapWidget, self).__init__(appserver, session) And again: def __init__(self, **kwargs): # … super(BaseDesktop, self).__init__(**kwargs) def __init__(self, **kwargs): # … super(BootstrapWidget, self).__init__(**kwargs) > class BootstrapDesktop(BootstrapWidget, BaseDesktop): > def __init__(self, appserver, session): > # there is NOTHING else here, it just connects bootstrap widget > implementation with desktop methods > super(BootstrapDesktop, self).__init__(appserver, session) If the ‘__init__’ method literally does nothing except call the superclass's method, don't define it in BootstrapDesktop at all. And likewise for the rest: pass along all remaining arguments, and don't write a do-nothing ‘__init__’. > Of course, I could explicitly call constructors of base classes. But > then I cannot use super() anywhere. Even not in places that are far > down in the class hierarchy. This results in typing all method > parameters many times, and also unnecesary refactoring. You avoid this by the ‘**kwargs’ idiom (no need to name arguments you don't explicitly handle), and by not writing any ‘__init__’ if you want the default behaviour (call the superclass's method). > This is time consuming and error prone. As pointed out in Raymond Hettinger's article , multiple inheritance is inherently tricky and there is an irreducible complexity that has to appear somewhere in your code. > In fact, I could never safely use super() in any case when the method > signature has changed. When signatures are changed, method resolution > must be done manually anyway. Use ‘**kwargs’, and always pass along the remaining ‘**kwargs’ minus-any-parameters-you-already-handled, to protect your existing classes from unnecessary signature changes. -- \ “Creativity can be a social contribution, but only in so far as | `\ society is free to use the results.” —Richard M. Stallman | _o__) | Ben Finney