introspection and functions

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

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?
(['arg1', 'arg2', 'arg3'], 'args', 'kw', ('toto',))


HTH
 
Y

yagyala

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
 
W

Wildemar Wildenburger

yagyala said:
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
 
J

James Stroud

yagyala said:
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

py> def doit(a, b, c, x=14):
.... pass
....
py> doit.func_code.co_argcount
4
py> doit.func_code.co_varnames
('a', 'b', 'c', 'x')
py> doit.func_defaults
(14,)

James
 
S

Scott David Daniels

yagyala said:
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)
 
A

Ayaz Ahmed Khan

"James Stroud" typed:
py> def doit(a, b, c, x=14):
... pass
...
py> doit.func_code.co_argcount
4
py> doit.func_code.co_varnames
('a', 'b', 'c', 'x')
py> doit.func_defaults
(14,)

Neat.
 
W

Wildemar Wildenburger

Ricardo said:
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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top