How to get the formal args of a function object?
Chris Rebert
clp2 at rebertia.com
Thu May 14 16:27:41 EDT 2009
On Thu, May 14, 2009 at 12:31 PM, kj <socyl at 987jk.com.invalid> wrote:
>
>
> Suppose that f is an object whose type is 'function'.
>
> Is there a way to find out f's list of formal arguments?
>
> The reason for this is that I'm trying to write a decorator and
> I'd like the wrapper to be able to check the number of arguments
> passed. Specifically, I'd like the wrapper to look as shown below:
>
> def _wrap(f):
> def wrapper(self, *params):
> n_expected = len(f.FORMAL_ARGS)
> n_received = len(params)
> if n_received is not n_expected:
> raise RuntimeError("Wrong number of arguments passed "
> "to %s" % f.__name__)
> return self.send_jsonrpc_request(f.__name__, params)
> return wrapper
>
> ...but I'm missing something like the hypothetical attribute
> FORMAL_ARGS above.
Take a look at inspect.getargspec(func):
http://docs.python.org/library/inspect.html#inspect.getargspec
Cheers,
Chris
--
http://blog.rebertia.com
> --
> NOTE: In my address everything before the first period is backwards;
> and the last period, and everything after it, should be discarded.
P.S. Have you considered making your address obfuscation just slightly
less of a pain in the ass? It's particularly annoying compared to
others'.
More information about the Python-list
mailing list