Get actual call signature?

Jarek Zgoda jzgoda at o2.usun.pl
Tue Mar 18 21:18:33 CET 2008


castironpi at gmail.com pisze:
> On Mar 18, 5:40 am, Jarek Zgoda <jzg... at o2.usun.pl> wrote:
>> Say, I have a function defined as:
>>
>> def fun(arg_one, arg_two='x', arg_three=None):
>>     pass
>>
>> Is there any way to get actual arguments that will be effectively used
>> when I call this function in various ways, like:
>>
>> fun(5) => [5, 'x', None]
>> fun(5, arg_three=['a', 'b']) => [5, 'x', ['a', 'b']]
>> fun(5, 'something') => [5, 'something', None]
>>
>> (et caetera, using all possible mixes of positional, keyword and default
>> arguments)
>>
>> I'd like to wrap function definition with a decorator that intercepts
>> not only passed arguments, but also defaults that will be actually used
>> in execution.
>>
>> If this sounds not feasible (or is simply impossible), I'll happily
>> throw this idea and look for another one. ;)
> 
> It evaluates to a substantial problem.  The combinations include
> things that Python disallows, such as double-spec. of keywords and
> spec'n of keys w/out a dictionary arg; as well as double-spec'ing of
> inspection.  How do you want to access the parameters?  What is the
> least redundant way?  P.S.  Does there exist a possible authority who
> doesn't want me to post this?

Well, after some thinking and research I found this much more
complicated than my first thoughts. However, I found that somebody
already wrote some code to solve similar problem and even described what
has to be done: http://wordaligned.org/articles/echo. Too bad for me,
the most interesting part relies on features introduced with Python 2.5,
while I am still on 2.4.
Anyway, basics still works and fortunately I am in control in both
function definitions and calls.

-- 
Jarek Zgoda
http://zgodowie.org/

"We read Knuth so you don't have to" - Tim Peters



More information about the Python-list mailing list