*args and **opts oddities (?)
Dinu Gherman
gherman at darwin.in-berlin.de
Thu Nov 29 03:47:49 EST 2001
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)
print args, opts # (2)
# result as expected, but in an unexpected way
def transform1(*args, **opts):
print 't', args, opts
return args[0], args[1] # ???
def call1(*args, **opts):
print args, opts # (1)
args, opts = transform1(args, opts)
print args, opts # (2)
## >>> call0('foo', 'bar', foobar=0)
## ('foo', 'bar') {'foobar': 0}
## t (('foo', 'bar'), {'foobar': 0}) {}
## (('foo', 'bar'), {'foobar': 0}) {}
## >>>
## >>> call1('foo', 'bar', foobar=0)
## ('foo', 'bar') {'foobar': 0}
## t (('foo', 'bar'), {'foobar': 0}) {}
## ('foo', 'bar') {'foobar': 0}
## >>>
More information about the Python-list
mailing list