Function argument conformity check

D

dlists.cad

Hi. I am looking for a way to check if some given set of (*args,
**kwds) conforms to the argument specification of a given function,
without calling that function.

For example, given the function foo:
def foo(a, b, c): pass

and some tuple args and some dict kwds, is there a way to tell if i
_could_ call foo(*args, **kwds) without getting an exception for those
arguments? I am hoping there is a way to do this without actually
writing out the argument logic python uses.

Thanks.
 
C

Cédric Lucantis

Hi,

Le Wednesday 18 June 2008 20:19:12 (e-mail address removed), vous avez écrit :
Hi. I am looking for a way to check if some given set of (*args,
**kwds) conforms to the argument specification of a given function,
without calling that function.

For example, given the function foo:
def foo(a, b, c): pass

and some tuple args and some dict kwds, is there a way to tell if i
_could_ call foo(*args, **kwds) without getting an exception for those
arguments? I am hoping there is a way to do this without actually
writing out the argument logic python uses.

Each function object is associated to a code object which you can get with
foo.func_code. Two of this object's attributes will help you: co_argcount and
co_varnames. The first is the number of arguments of the function, and the
second a list of all the local variables names, including the arguments
(which are always the first items of the list). When some arguments have
default values, they are stored in foo.func_defaults (and these arguments are
always after non-default args in the co_argnames list).

Finally, it seems that some flags are set in code.co_flags if the function
accepts varargs like *args, **kwargs, but I don't know where these are
defined.

Note that I never found any doc about that and merely guessed it by playing
with func objects, so consider all this possibly wrong or subject to change.
 
D

dlists.cad

Hi,

Le Wednesday 18 June 2008 20:19:12 (e-mail address removed), vous avez écrit :




Each function object is associated to a code object which you can get with
foo.func_code. Two of this object's attributes will help you: co_argcount and
co_varnames. The first is the number of arguments of the function, and the
second a list of all the local variables names, including the arguments
(which are always the first items of the list). When some arguments have
default values, they are stored in foo.func_defaults (and these arguments are
always after non-default args in the co_argnames list).

Finally, it seems that some flags are set in code.co_flags if the function
accepts varargs like *args, **kwargs, but I don't know where these are
defined.

Note that I never found any doc about that and merely guessed it by playing
with func objects, so consider all this possibly wrong or subject to change.

I am aware of these attributes, although you can get them all in a
more organized form using the getfullargspec function in the inspect
module from the standard library.

The problem is that using these attributes, I would essentially have
to re-write the logic python uses when calling a function with a given
set of arguments. I was hoping there is a way to get at that logic
without rewriting it.
 
M

Matthew Woodcraft

The problem is that using these attributes, I would essentially have
to re-write the logic python uses when calling a function with a
given set of arguments. I was hoping there is a way to get at that
logic without rewriting it.

I don't think there is. I ended up rewriting it when I wanted to do a
similar thing.

-M-
 
G

George Sakkis

I am aware of these attributes, although you can get them all in a
more organized form using the getfullargspec function in the inspect
module from the standard library.

The problem is that using these attributes, I would essentially have
to re-write the logic python uses when calling a function with a given
set of arguments. I was hoping there is a way to get at that logic
without rewriting it.

Sure; copy it from someone that has already done it ;-)

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/551779

HTH,
George
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
Hi. I am looking for a way to check if some given set of (*args,
**kwds) conforms to the argument specification of a given function,
without calling that function.

import inspect
help(inspect.getargspec)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top