Is it possible to determine what a function needs for parameters -
rh0dium
steven.klass at gmail.com
Wed May 2 12:36:05 EDT 2007
On May 2, 7:49 am, Bruno Desthuilliers <bruno.
42.desthuilli... at wtf.websiteburo.oops.com> wrote:
> Yes - using inspect.getargspec. I don't have example code at hand yet,
> but it's not really complicated.
Viola!! Hey this works!! Now I have modified my code to do this - way
cool (still kind of a mess though)
args, varargs, varkw, defs =
inspect.getargspec(self.function)
# Deal with just some args
if varargs is None and varkw is None:
result=self.function(input)
# Deal with *args
if varkw is None and varargs is not None and len(args) >
0:
result=self.function(input[0:-1], *input[-1])
if varkw is None and varargs is not None and len(args)==0:
result=self.function(*input[0])
# Deal with *kwargs
if varkw is not None and varargs is not None and len(args)
> 0:
result=self.function(input[0:-2], *input[-2],
**input[-1])
if varkw is not None and varargs is not None and
len(args)==0:
result=self.function(*input[-2], **input[-1])
if varkw is not None and varargs is None and len(args) >
0:
result=self.function(input[0:-1], **input[-1])
if varkw is not None and varargs is None and len(args) ==
0:
result=self.function(**input[0])
Now this worked until I threw a function which looked like this
def func5( input1, input2, input3 )
pass
So it barfed because of this..
if varargs is None and varkw is None:
result=self.function(input)
but all of the parameters were lumped as a list so input1 contained
them all...
A small tweak turned into this..
if varargs is None and varkw is None:
if isinstance(input, tuple):
result=self.function(*input)
else:
result=self.function(input)
But now I suppose I need to do this for all of them but that will
break my other logic...
Yuck - I have to be missing something here.
More information about the Python-list
mailing list