471,316 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,316 software developers and data experts.

introspection and functions

Hi. I would like to be able to tell, at run time, how many parameters
a function requires. Ideally I would like to be able to tell which are
optional as well. I've tried looking at the functions attributes, but
haven't found one that helps in this. How can I do this?

Thanks

Aug 22 '07 #1
7 988
yagyala wrote:
Hi. I would like to be able to tell, at run time, how many parameters
a function requires. Ideally I would like to be able to tell which are
optional as well. I've tried looking at the functions attributes, but
haven't found one that helps in this. How can I do this?
I've never used it before, but there is the "inspect" module.
<url:http://docs.python.org/lib/module-inspect.html>.
That any good?

/W
Aug 22 '07 #2
yagyala wrote:
Hi. I would like to be able to tell, at run time, how many parameters
a function requires. Ideally I would like to be able to tell which are
optional as well. I've tried looking at the functions attributes, but
haven't found one that helps in this. How can I do this?

Thanks
pydef doit(a, b, c, x=14):
.... pass
....
pydoit.func_code.co_argcount
4
pydoit.func_code.co_varnames
('a', 'b', 'c', 'x')
pydoit.func_defaults
(14,)

James
Aug 22 '07 #3
yagyala a écrit :
Hi. I would like to be able to tell, at run time, how many parameters
a function requires. Ideally I would like to be able to tell which are
optional as well. I've tried looking at the functions attributes, but
haven't found one that helps in this. How can I do this?
>>def myfunc(arg1, arg2, arg3='toto', *args, **kw): pass
....
>>import inspect
inspect.getargspec(myfunc)
(['arg1', 'arg2', 'arg3'], 'args', 'kw', ('toto',))
HTH
Aug 23 '07 #4
yagyala wrote:
Hi. I would like to be able to tell, at run time, how many parameters
a function requires. Ideally I would like to be able to tell which are
optional as well. I've tried looking at the functions attributes, but
haven't found one that helps in this. How can I do this?

Thanks
This really only will work for those functions that are simply
constructed. You are better off not trying to do this, or code
like the following will confound your code:

from functools import partial

def somefunction(a=23, b=14, c='hi'):
print 'Finally'

f = partial(somefunction, b=13)
g = partial(f, a=19)
h = partial(g, c=123)
print whatargs(h)
Aug 23 '07 #5
"James Stroud" typed:
pydef doit(a, b, c, x=14):
... pass
...
pydoit.func_code.co_argcount
4
pydoit.func_code.co_varnames
('a', 'b', 'c', 'x')
pydoit.func_defaults
(14,)
Neat.

--
Ayaz Ahmed Khan

I have not yet begun to byte!
Aug 23 '07 #6
Ayaz Ahmed Khan wrote:
"James Stroud" typed:
>pydef doit(a, b, c, x=14):
... pass
...
pydoit.func_code.co_argcount
4
pydoit.func_code.co_varnames
('a', 'b', 'c', 'x')
pydoit.func_defaults
(14,)

Neat.
How do you know the 14 corresponds to x ?
Aug 23 '07 #7
Ricardo Aráoz wrote:
Ayaz Ahmed Khan wrote:
>"James Stroud" typed:
>>pydef doit(a, b, c, x=14):
... pass
...
pydoit.func_code.co_argcount
4
pydoit.func_code.co_varnames
('a', 'b', 'c', 'x')
pydoit.func_defaults
(14,)
Neat.
How do you know the 14 corresponds to x ?
Well, there is one optional argument (len(doit.func_defaults)==1) and
those *must* come after mendatory arguments. More generally: If a
function has n optional args, they're the n last ones. There is never
any ambiguity.

/W
Aug 24 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Vsevolod (Simon) Ilyushchenko | last post: by
4 posts views Thread by Benjamin Rutt | last post: by
reply views Thread by Steven T. Hatton | last post: by
4 posts views Thread by Steven T. Hatton | last post: by
1 post views Thread by James Geurts | last post: by
3 posts views Thread by James Stroud | last post: by
14 posts views Thread by Dave Rahardja | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.