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


Groups > comp.lang.python > #56504

Re: super in Python 3 and variadic arguments

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <nedbat@gmail.com>
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; 'argument': 0.05; 'subject:Python': 0.06; 'arguments': 0.09; 'way:': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'stored': 0.12; '*args': 0.16; '*args):': 0.16; 'class:': 0.16; 'inspects': 0.16; 'marco': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; 'environment': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'code': 0.31; '"",': 0.31; 'object.': 0.31; 'file': 0.32; 'class': 0.32; 'regular': 0.32; '(most': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'entry': 0.36; 'recent': 0.39; 'does': 0.39; 'how': 0.40; 'first': 0.61; 'name': 0.63; 'to:addr:gmail.com': 0.65; 'determine': 0.67; '11:44': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=+leoqtskhmM4RAhq4taeATMt9As5B+W+c0TLcFBZ+Ns=; b=0c//9aD+A/NtWf8DFz8K6Mur5ph9x/KTIHlE0y621t2BpI1kdGlvuX0KV/7RJ8pO+t fjFBl9FZWWREPe1Jd/r8HLoD/BcxNL62H7tURg9XFlg0Hwx4XQa9yUu2c3qUEVFLvwgN cpdx5Ozafgtb5/VetYuAyPXFc0woPzjswbBsild04FvA5Yk1ZWtlz1Paa4O+np7RXVVr Qks55r+3djHL14xeKaawMYw3SD8a4LVcyUq7z5xfIYOQ1DYHo5AFQJIeSljEIfu9J/KV ZF8auJUZ3urdPO9FXOgg1sfUsGuC+JTL/KAzOjzpJBJ0+gv/0lWnx4EOpnMV5dTVVf8u X9DQ==
X-Received by 10.221.40.10 with SMTP id to10mr6329401vcb.22.1381337234772; Wed, 09 Oct 2013 09:47:14 -0700 (PDT)
Sender Ned Batchelder <nedbat@gmail.com>
Date Wed, 09 Oct 2013 12:47:13 -0400
From Ned Batchelder <ned@nedbatchelder.com>
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130801 Thunderbird/17.0.8
MIME-Version 1.0
To Marco Buttu <marco.buttu@gmail.com>
Subject Re: super in Python 3 and variadic arguments
References <l33tlf$rr1$1@speranza.aioe.org>
In-Reply-To <l33tlf$rr1$1@speranza.aioe.org>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.902.1381337680.18130.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1381337680 news.xs4all.nl 15944 [2001:888:2000:d::a6]:49133
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:56504

Show key headers only | View raw


On 10/9/13 11:44 AM, Marco Buttu wrote:
> Given this class:
>
> >>> class A:
> ...     def afoo(*args):
> ...         print(args)
>
> in Python 3 we can write the following class:
>
> >>> class B(A):
> ...     def bfoo(*args):
> ...         super(B, args[0]).afoo(*args[1:])
> ...
> >>> B().bfoo(1, 2, 3)
> (<__main__.B object at 0x7f5b3bde48d0>, 1, 2, 3)
>
>
> without giving arguments to super, in this way:
>
> >>> class B(A):
> ...     def bfoo(self, *args):
> ...         super().afoo(*args)
> ...
> >>> B().bfoo(1, 2, 3)
> (<__main__.B object at 0x7f5b3bdea0d0>, 1, 2, 3)
>
> But it does not work in this case:
>
> >>> class B(A):
> ...     def bfoo(*args):
> ...         super().afoo(*args[1:])
> ...
> >>> B().bfoo(1, 2, 3)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "<stdin>", line 3, in bfoo
> RuntimeError: super(): no arguments
>
> How come?

The no-args super() call inspects the calling environment to determine 
the class and self.  "self" is the first local name stored in 
frame.f_code.co_localsplus, but *args doesn't put "args" into that entry 
of the code object.  Basically, super() is looking for the first regular 
argument in the function.

--Ned.

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


Thread

super in Python 3 and variadic arguments Marco Buttu <marco.buttu@gmail.com> - 2013-10-09 17:44 +0200
  Re: super in Python 3 and variadic arguments Ned Batchelder <ned@nedbatchelder.com> - 2013-10-09 12:47 -0400
    Re: super in Python 3 and variadic arguments Marco Buttu <marco.buttu@gmail.com> - 2013-10-10 09:22 +0200
      Re: super in Python 3 and variadic arguments Ned Batchelder <ned@nedbatchelder.com> - 2013-10-10 07:04 -0400
        Re: super in Python 3 and variadic arguments Marco Buttu <marco.buttu@gmail.com> - 2013-10-10 14:54 +0200
        Re: super in Python 3 and variadic arguments Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-11 02:11 +0000
          Re: super in Python 3 and variadic arguments Ian Kelly <ian.g.kelly@gmail.com> - 2013-10-10 20:33 -0600
            Re: super in Python 3 and variadic arguments Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-11 03:00 +0000
              Re: super in Python 3 and variadic arguments Chris Angelico <rosuav@gmail.com> - 2013-10-11 17:08 +1100
            Re: super in Python 3 and variadic arguments Marco Buttu <marco.buttu@gmail.com> - 2013-10-11 08:17 +0200
          Re: super in Python 3 and variadic arguments Marco Buttu <marco.buttu@gmail.com> - 2013-10-11 08:15 +0200

csiph-web