arguments problem~

Francis Avila francisgavila at yahoo.com
Wed Dec 24 23:45:02 EST 2003


black wrote in message ...

class B(A):
    def __init__(self, *args, **kw):
        A.__init__(self, args, kw)

Should be A.__init__(self, *args, **kw).

To illustrate:

>>> def f(*args, **kwargs):
...     return [args, kwargs]
...
>>> f(1, 2, b=3)
[(1, 2), {'b': 3}]
>>> a = f(1, 2, b=3)
>>> f(a)
[([(1, 2), {'b': 3}],), {}]
>>> f(*a) #Your error
[((1, 2), {'b': 3}), {}]
>>> f(*a[0], **a[1])
[(1, 2), {'b': 3}]

In a calling context, * and ** do the opposite of what they do in a function
definition context: they unpack tuple/lists and dicts into distinct
arguments and keyword arguments.  By not calling A.__init__ with unpacked
arguments, you literally passed a tuple and a dict, and in the function
these got grouped together in args.

It's in the docs somewhere, I swear. :)
--
Francis Avila





More information about the Python-list mailing list