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

Date 2013-10-09 12:47 -0400
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: super in Python 3 and variadic arguments
References <l33tlf$rr1$1@speranza.aioe.org>
Newsgroups comp.lang.python
Message-ID <mailman.902.1381337680.18130.python-list@python.org> (permalink)

Show all headers | 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