Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109898
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Overriding methods inherited from a superclass with methods from a mixin |
| Date | Mon, 13 Jun 2016 07:17:59 -0600 |
| Lines | 45 |
| Message-ID | <mailman.36.1465823921.2288.python-list@python.org> (permalink) |
| References | <2a1088f1-3bdb-4b21-80b5-8cc640d3bceb@googlegroups.com> <87bn351ryl.fsf@handshake.de> <CALwzidnVQAOsjWdg0eA0qw-PzaL3ugwc5A0ncS-zWw_F+pudyQ@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de 3uc3nLOWd2QbOCofoDbWywkfKn3nukIVCoY2ZdGY3Rzw== |
| Return-Path | <ian.g.kelly@gmail.com> |
| 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; 'classes,': 0.05; 'attributes': 0.07; 'constructor': 0.07; 'exception.': 0.07; 'override': 0.07; 'assumed': 0.09; 'derived': 0.09; 'determines': 0.09; 'explicitely': 0.09; 'inherited': 0.09; 'lookup': 0.09; 'satisfy': 0.09; 'subclass': 0.09; 'subclasses': 0.09; 'throw': 0.09; 'received:209.85.218': 0.10; 'python': 0.10; 'subject: \n ': 0.15; '1:13': 0.16; '2016': 0.16; "base's": 0.16; 'inheritance': 0.16; 'mro': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'looked': 0.16; 'do.': 0.22; 'fairly': 0.22; 'controlled': 0.22; 'pass': 0.22; 'trying': 0.22; 'am,': 0.23; 'defined': 0.23; 'thus': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'module': 0.25; "i've": 0.25; 'appear': 0.26; 'right.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'correct': 0.28; 'function': 0.28; 'resolution': 0.28; '(it': 0.29; '13,': 0.29; 'about.': 0.29; 'finds': 0.29; "i'm": 0.30; 'classes': 0.30; 'class.': 0.30; 'rules': 0.31; 'up.': 0.32; 'statement': 0.32; 'useful': 0.33; 'class': 0.33; 'definition': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'skip:. 20': 0.35; 'something': 0.35; "isn't": 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'possible': 0.36; '(and': 0.36; "wasn't": 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'listing': 0.38; 'received:209': 0.38; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'your': 0.60; 'determine': 0.61; 'email addr:gmail.com': 0.62; 'show': 0.62; 'details': 0.62; 'complex,': 0.84; 'complex.': 0.84; 'fortunately,': 0.84; 'simple:': 0.84; 'to:name:python': 0.84; 'url:show': 0.84; 'worried': 0.84; 'absolutely': 0.88 |
| 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; bh=PadaUR4FtC1mS+oJRgiDWJi1Dfqq5sEoujCviqKOHpw=; b=TojG1PRzrcMzVxaVFdtZ/SARYOathApvCXRu2a5vSu9n61/RqBdFmq6u0CZDQZn1/k XeDdRZeRHIE2t25hqihnDkTWVHA2S9d2PQpeShLNczcWUiA9rU0ld5o1bP82aobPIvvc BjY2pIB87V6Kz6uvDhoKDfYJBjpUT12OEO1TcFFhJbhDYcaK55Kiq3+Ah9W6msoWCGgS y9hRQJQ1/r3HYvd8IrYVH18eHyOjsjr4GTd4ZNc0Bj5PTHNUzTm0VzMFi37yyyU+nLOU vokUTeLFkkdcbphGInX0l7Yy/OUftqt3XunMIpR4/SLfvoX4A4c0IdQ1DdMvbsKYjiaH gQgg== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=PadaUR4FtC1mS+oJRgiDWJi1Dfqq5sEoujCviqKOHpw=; b=Ta5NwhmoVlXUlhRZbobDzMXKSat4z5jEwkqftxrmrqfyoc2E367M3uLsm8Bf771xfx GhjvNIGcZ7k681/bWIMxn/pTCXhT2epEyRlyqd+Nj9t1rNNy2BFrvlxECKHkpimuxHNW wGN/uLgdJnsh3S2xjTsb1b56wko5LQ+cfX+P5cRr+D7AuVjprEo21AUQ/LNQRMl8Nq+9 mrpox7rSVtkQE6fxd3DP61wGmYbJUmfmHnFb1hPKmt2nQFyPkfADy55dcb97GiKo8KbM tF5h61oianW4+v4uEsaMQNvOB2OUXcknh50uAFQifx6Bgx7UUHbd3Qj5gBTU9p25ax4I 6CeQ== |
| X-Gm-Message-State | ALyK8tKdInEpVOXa8I7mHTWtpNhqXy3IO64bOvRxgWhT5zGkgjlww16aKbOODTs8KFEqEadM13TBnL1i0vycCg== |
| X-Received | by 10.202.77.22 with SMTP id a22mr6246083oib.159.1465823918894; Mon, 13 Jun 2016 06:18:38 -0700 (PDT) |
| In-Reply-To | <87bn351ryl.fsf@handshake.de> |
| 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 | <CALwzidnVQAOsjWdg0eA0qw-PzaL3ugwc5A0ncS-zWw_F+pudyQ@mail.gmail.com> |
| X-Mailman-Original-References | <2a1088f1-3bdb-4b21-80b5-8cc640d3bceb@googlegroups.com> <87bn351ryl.fsf@handshake.de> |
| Xref | csiph.com comp.lang.python:109898 |
Show key headers only | View raw
On Mon, Jun 13, 2016 at 1:13 AM, dieter <dieter@handshake.de> wrote:
> alanqueiros@gmail.com writes:
>
>> I'm trying to override methods inherited from a superclass by methods defined in a mixin class.
>> Here's an sscce:
>> https://bpaste.net/show/6c7d8d590658 (never expires)
>>
>> I've had problems finding the proper way to do that, since at first the base class wasn't to the right and I've assumed the correct order was from left to right.
>
> The class' MRO ("Method Resolution Order") determines in which
> order attributes are looked up.
> Either, you must order your base classes in such a way that the MRO
> controlled lookup finds the methods you want to be used or
> you must explicitely put a definition for those methods in your
> derived class (it may have the form "overridden_method = <BaseClass>.overridden_method").
>
> The rules to determine the MRO are complex. The "inspect" module contains
> a function ("get_mro") to show the MRO of a given class. Use it
> to get your inheritance order right.
The details are complex, but there are two fairly simple principles
that can be relied upon:
1) Subclasses always appear before their superclasses in the MRO.
2) When a class inherits from multiple base classes, they will always
appear in the MRO in the order they appear in the class statement from
left-to-right.
Note that neither of these rules guarantee that the classes will
appear *sequentially* in the MRO.
So in the case of mixins, listing the mixin class first is absolutely correct:
class Derived(Mixin, Base): pass
This ensures that Mixin will always appear before Base in the MRO (and
thus override Base's methods) for Derived and for any subclass of
Derived.
It is possible to concoct elaborate inheritance hierarchies in which
it is not possible to come up with an MRO that will satisfy both 1)
and 2) above. In such cases, it is also useful to know what Python
will do. Fortunately, the answer to that is also simple: the type
constructor will throw an exception. So this isn't something that
really needs to be worried about.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Overriding methods inherited from a superclass with methods from a mixin alanqueiros@gmail.com - 2016-06-12 20:55 -0700
Re: Overriding methods inherited from a superclass with methods from a mixin dieter <dieter@handshake.de> - 2016-06-13 09:13 +0200
Re: Overriding methods inherited from a superclass with methods from a mixin Peter Otten <__peter__@web.de> - 2016-06-13 09:59 +0200
Re: Overriding methods inherited from a superclass with methods from a mixin Ian Kelly <ian.g.kelly@gmail.com> - 2016-06-13 07:17 -0600
Re: Overriding methods inherited from a superclass with methods from a mixin Michael Selik <michael.selik@gmail.com> - 2016-06-13 17:32 +0000
Re: Overriding methods inherited from a superclass with methods from a mixin alanqueiros@gmail.com - 2016-06-13 11:42 -0700
Re: Overriding methods inherited from a superclass with methods from a mixin Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-06-14 10:28 +1200
Re: Overriding methods inherited from a superclass with methods from a mixin Michael Selik <michael.selik@gmail.com> - 2016-06-13 22:50 +0000
Re: Overriding methods inherited from a superclass with methods from a mixin alanqueiros@gmail.com - 2016-06-13 16:44 -0700
Re: Overriding methods inherited from a superclass with methods from a mixin Michael Selik <michael.selik@gmail.com> - 2016-06-14 00:13 +0000
csiph-web