super in Python 3 and variadic arguments
Marco Buttu
marco.buttu at gmail.com
Wed Oct 9 11:44:50 EDT 2013
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?
--
Marco Buttu
More information about the Python-list
mailing list