*args and **opts oddities (?)
Michael Hudson
mwh at python.net
Thu Nov 29 05:44:17 EST 2001
Dinu Gherman <gherman at darwin.in-berlin.de> writes:
> Hi,
>
> I'm trying to transform *args and **opts parameters and pretend
> they were passed in some transformed way. What I find is an un-
> expected phenomenon that makes me use indices in the code below
> (in function transform1) to get the behaviour I want. Is there
> any good explanation for this?
>
> Thanks,
>
> Dinu
>
>
> # unexpected result: lines (1) and (2) are not equal
>
> def transform0(*args, **opts):
> print 't', args, opts
> return args, opts
>
> def call0(*args, **opts):
> print args, opts # (1)
> args, opts = transform0(args, opts)
Think about what this does; it calls transform0 with two positional
arguments, the tuple `args', and the dictionary `opts'. These then go
into the `*args' parameter of transform0, and the dictionary `opts' is
empty -- because there weren't any keyword arguments.
Did you want to write
args, opts = transform0(*args, **opts)
?
Cheers,
M.
--
The Oxford Bottled Beer Database heartily disapproves of the
excessive consumption of alcohol. No, really.
-- http://www.bottledbeer.co.uk/beergames.html
(sadly now 404 compliant)
More information about the Python-list
mailing list