470,580 Members | 2,327 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,580 developers. It's quick & easy.

Detecting arguments of a function - possible?

I am trying to write a tool to examine a function (I'd like it
to work with pyc files only). So here are a few questions I
have; any pointers would be very welcome.

Can I determine the number of arguments required of a function?
Is there a way to detect is the function will throw an exception
(I don't care under what conditions just that it is possible)?

Thanks in advance...
Chas.
Mar 17 '06 #1
3 1052
Chance Ginger wrote:
I am trying to write a tool to examine a function (I'd like it
to work with pyc files only). So here are a few questions I
have; any pointers would be very welcome.

Can I determine the number of arguments required of a function?
See inspect.getargspec()
Is there a way to detect is the function will throw an exception
(I don't care under what conditions just that it is possible)?


If you mean something like Java's throws declaration, no, Python doesn't
have that. Any function can raise an exception. IIUC even something as
simple as
def foo(): pass
can raise KeyboardInterrupt

Kent
Mar 17 '06 #2
> I am trying to write a tool to examine a function (I'd like it
to work with pyc files only). So here are a few questions I
have; any pointers would be very welcome.
Try playing around with reflection - the function-code reveals some
properties of the code.

for example
import inspect
def foo(a, b=1): .... c = a * b
.... inspect.getargs(foo)
inspect.getargspec(foo)

(['a', 'b'], None, None, (1,))

Can I determine the number of arguments required of a function?
Is there a way to detect is the function will throw an exception
(I don't care under what conditions just that it is possible)?


No. You can disassemble a function and see if it contains a raise-statement.
But as it is perfectly legal to say:

raise some_random_value()

you have absolutely no idea what will be raised. And of course every call to
another function and even some bytecodes (like division, which may result
in ZeroDivsionError) can raise an exception.

Diez
Mar 17 '06 #3
>>>> inspect.getargs(foo)

Copy-and-paste error. Ignore that line.
Diez
Mar 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.