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


Groups > comp.lang.python > #109437

Re: Multiple inheritance, super() and changing signature

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: Multiple inheritance, super() and changing signature
Date Sat, 04 Jun 2016 06:16:27 +1000
Lines 59
Message-ID <mailman.135.1464985027.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> <85k2i65upp.fsf@benfinney.id.au> <b21d46a9-763a-fe38-b539-77867fcdcdc9@shopzeus.com> <85fusu58pw.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 rprsbXJQvAtD6N7qlWL/zQV80tUFTD2ZyCSRbd3QMY+Q==
Cancel-Lock sha1:ItcwjzVWzz7lPm0t25Rx7C5biDQ=
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'initialize': 0.05; 'needed,': 0.05; '*args,': 0.07; 'attributes': 0.07; 'override': 0.07; '__init__': 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; 'python': 0.10; 'def': 0.13; '*additional*': 0.16; 'initialiser': 0.16; 'instances,': 0.16; "method's": 0.16; 'omitting': 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; 'right:': 0.16; 'sub-class': 0.16; 'subject:changing': 0.16; 'useless': 0.16; 'instance,': 0.18; '(in': 0.18; 'handles': 0.20; 'affected': 0.22; 'explicit': 0.22; 'simpler': 0.22; 'pass': 0.22; 'specified': 0.23; 'this:': 0.23; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'parameters': 0.27; 'defining': 0.27; 'initial': 0.28; '**kwargs)': 0.29; '**kwargs):': 0.29; 'follows': 0.29; 'spam,': 0.29; 'allows': 0.30; 'class.': 0.30; 'skip:_ 10': 0.32; 'statement': 0.32; 'point': 0.33; 'useful': 0.33; 'class': 0.33; 'except': 0.34; 'skip:d 20': 0.34; 'next': 0.35; 'instance': 0.35; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'expect': 0.37; 'method': 0.37; 'setting': 0.37; 'received:org': 0.37; 'seem': 0.37; 'things': 0.38; '(with': 0.38; 'difference': 0.38; 'anything': 0.38; 'does': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'default': 0.61; 'real': 0.62; 'between': 0.65; 'skip:\xe2 10': 0.70; '8bit%:43': 0.72; 'estate': 0.76; '_o__)': 0.84; 'difference.': 0.84; 'handled,': 0.84; 'nagy': 0.84; 'received:125': 0.84; '\xe2\x80\x9cthe': 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 <bignose+hates-spam@benfinney.id.au>
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 <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 <85fusu58pw.fsf@benfinney.id.au>
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> <85k2i65upp.fsf@benfinney.id.au> <b21d46a9-763a-fe38-b539-77867fcdcdc9@shopzeus.com>
Xref csiph.com comp.lang.python:109437

Show key headers only | View raw


Nagy László Zsolt <gandalf@shopzeus.com> writes:

> So you are right: the custom __init__ in the BootstrapDesktop class is
> not really needed, and does not do anything useful in that particular
> class.

I disagree: setting initial attributes is a normal and useful case for
defining a custom initialiser.

> My original statement was this: "I have to initialize some default
> attributes", and for that I need to pass arguments.

Yes. If you need to initialise the instance, that's what a custom
‘__init__’ is for.

> The truthness of this statement is not affected by adding a useless
> override of a method (with the very same parameters). Even though I
> see that you are right in what you wrote, I think I don't understand
> the point because it seem unrelated.

My point was rather that if the custom initialiser does *nothing* except
call the superclass's initialiser, then there's no purpose to writing
the custom initialiser.


If you're writing a custom initialiser that handles two additional
parameters, then those parameters should not be present when you call
the super() method's initialiser::

    # You specified Python 3, which allows simpler syntax.

    class LoremIpsum:
        def __init__(self, spam, *args, **kwargs):
            do_something_important_with(spam)
            super().__init__(*args, **kwargs)

    class DolorSitAmet(LoremIpsum):
        def __init__(self, spam, eggs=4, beans=None, *args, **kwargs):
            self.eggs = eggs
            self.beans = beans
            super().__init__(spam, *args, **kwargs)

So the sub-class follows the Liskov Substitution Principle: every
DolorSitAmet instance should be substitutable for LoremIpsum instances,
and users that only expect a LoremIpsum instance should not need to know
any difference.

This entails that its methods (in this case the initialiser) will accept
the same parameters as ‘LoremIpsum.__init__’, and do the same things
with those parameters. We make that explicit by omitting the
*additional* parameters that we already handled, ‘eggs’ and ‘beans’,
from the next call in the chain.

-- 
 \        “The difference between religions and cults is determined by |
  `\                      how much real estate is owned.” —Frank Zappa |
_o__)                                                                  |
Ben Finney

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