forwarding *arg parameter

Tuomas tuomas.vesterinen at pp.inet.fi
Sun Nov 5 17:51:00 EST 2006


Dennis Lee Bieber wrote:
> On Sun, 05 Nov 2006 17:42:30 GMT, Tuomas <tuomas.vesterinen at pp.inet.fi>
> declaimed the following in comp.lang.python:
> 
> 
> 
>>I am looking a shorter way to do the above in the case:
>>
>>def g(*arg):
>>     return arg
>>
>>def f(*arg):
>>     return g(arg)
>>
>>How can g know if it is called directly with (('foo', 'bar'),) or via f 
> 
> 
> 	Typically, the responsibility should be on the CALLER, not the
> CALLED..
> 
> 
>>>>def g(*arg):
> 
> ... 	return arg
> ... 
> 
>>>>def f(*arg):
> 
> ... 	return g(*arg)	#<<<<<<<< unpack tuple on call
> ... 
> 
>>>>f("a", 1, 2)
> 
> ('a', 1, 2)
> 
> 
> 	Note how f() is calling g() using an * -- Since f() "knows" that its
> arguments were "packed" it calls g() with an unpack marker. Then g()
> gets the arguments via whatever scheme it was coded to use.
> 
> 
>>>>def f(*arg):
> 
> ... 	return g(arg)	#<<<<<<<<<< no tuple unpack
> ... 
> 
>>>>f("a", 1, 2)
> 
> (('a', 1, 2),)
> 
>  
I fylly agree with tis: "Typically, the responsibility should be on the 
CALLER, not the CALLED..". I just don't know how to unpack *arg for 
calling g. I can get the len(arg), but how to formulate an unpacked call 
g(arg[0], arg[1], ..). Building a string for eval("g(arg[0], arg[1], 
..)") seems glumsy to me.

TV



More information about the Python-list mailing list