How to get the formal args of a function object?
norseman
norseman at hughes.net
Thu May 14 17:15:05 EDT 2009
kj 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.
>
> TIA!
>
> Kynn
>
========================================
I've never tried that approach. But the attempt brings back memories.
Mind you I was using a compiler, not an interpreter.
Language was assembly
it outlines like this:
1st time called:
compile actual code # boo-koo bytes
if token count test OK
compile the call to the code # just the call (1 or 2 words)
else
print current program address
compile in a string denoting error "ERROR-ERROR"
to force a quit (crash)
2nd time and on
do not compile actual code # Zero bytes
if token count test OK
compile the call to the code # just the call (1 or 2 words)
else
print current program address
compile in a string denoting error "ERROR-ERROR"
to force a quit (crash)
Helps to print the assembly 'prn' file and look for your error flag
BEFORE trying to run program. :)
compiler token stack (storage) delta before/after packet loaded is
tested with size expected and compiler directives adjusted accordingly.
I have no idea how to do this with an interpreter. Maybe someone who
reads this can figure it out. If so - I want a copy!!!! (Please.)
Even:
def funct_foo(t1,t2,t3):
call_token_check(3) will not tell the incoming number
they are on a stack or something
somewhere else in memory.
With an interpreter this becomes an expensive overhead. Mine was only
costly at compile time. Not at run time. And it shortened the code and
the development time.
Steve
More information about the Python-list
mailing list