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


Groups > comp.lang.python > #6810

Re: Updated blog post on how to use super()

From Chris Torek <nospam@torek.net>
Newsgroups comp.lang.python
Subject Re: Updated blog post on how to use super()
Date 2011-06-01 17:43 +0000
Organization None of the Above
Message-ID <is5tnn01p0f@news4.newsguy.com> (permalink)
References <80476fba-1b57-4bb1-9d7d-391edaf3042d@22g2000prx.googlegroups.com> <is5dbe$n76$1@speranza.aioe.org> <mailman.2368.1306946563.9059.python-list@python.org> <is5qd7$t5b$1@speranza.aioe.org>

Show all headers | View raw


Summary: super(cls, data) in a method gets you the "next" handler
for a given class "cls" and an instance "data" that has derived
from that class at some point.  In Python 2 you must spell out the
names of the class and instance (normally "self") explicitly, while
Python 3 grabs, at compile time, the class from the lexically
enclosing class, and the instance from the first argument of the
method that invokes "super".

The "next" handler depends on the instance's __mro__.  If all
your classes use at most single inheritance, the "next" handler
in class Cls1 is easy to predict:

    class Cls1(Cls2):

Any instance of Cls1 always has Cls2 as its "next", so:

        def method(self, arg1, arg2):
            ...
            Cls2.method(self, arg1_mutated, arg2_mutated)
            ...

works fine.  But if you use multiple inheritance, the next method
is much harder to predict.  If you have a working "super", you
can use:

            super().method(self, arg1_mutated, arg2_mutated)

and it will find the correct "next method" in all cases.

In article <is5qd7$t5b$1@speranza.aioe.org>
Billy Mays  <noway@nohow.com> wrote:
>What it does is clear to me, but why is it interesting or special isn't. 
>  This looks like a small feature that would be useful in a handful of 
>cases.

Indeed: it is useful when you have multiple inheritance, which for
most programmers, is a "handful of cases".

However, provided you *have* the Py3k super() in the first place,
it is also trivial and obviously-correct to write:

        super().method(...)

whereas writing:

        NextClass.method(...)

requires going up to the class definition to make sure that
"NextClass" is indeed the next class, and hence -- while usually
no more difficult to write -- less obviously-correct.

Moreover, if you write the easy-to-write obviously-correct
"super().method", *your* class may now be ready for someone
else to use in a multiple-inheritance (MI) situation.  If you type
in the not-as-obviously-correct "NextClass.method", *your* class
is definitely *not* ready for someone else to use in that MI
situation.

(I say "may" be ready for MI, because being "fully MI ready" requires
several other code discipline steps.  The point of super() -- at
least when implemented nicely, as in Py3k -- is that it makes it
easy -- one might even say "super easy" :-) -- to write your code
such that it is obviously correct, and also MI-friendly.)
-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)      http://web.torek.net/torek/index.html

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


Thread

Updated blog post on how to use super() Raymond Hettinger <python@rcn.com> - 2011-05-31 19:44 -0700
  Re: Updated blog post on how to use super() Ben Finney <ben+python@benfinney.id.au> - 2011-06-01 14:26 +1000
  Re: Updated blog post on how to use super() Billy Mays <noway@nohow.com> - 2011-06-01 09:03 -0400
    Re: Updated blog post on how to use super() Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-01 10:42 -0600
      Re: Updated blog post on how to use super() Billy Mays <noway@nohow.com> - 2011-06-01 12:46 -0400
        Re: Updated blog post on how to use super() Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-01 11:06 -0600
        Re: Updated blog post on how to use super() Chris Torek <nospam@torek.net> - 2011-06-01 17:43 +0000
    Re: Updated blog post on how to use super() Duncan Booth <duncan.booth@invalid.invalid> - 2011-06-02 20:58 +0000

csiph-web