473,325 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 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 1050
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vsevolod (Simon) Ilyushchenko | last post by:
Hi, Last year I have written a Perl module to serve as a backend to Macromedia Flash applications: http://www.simonf.com/amfperl I have just rewritten it in Python, which I have not used...
4
by: Benjamin Rutt | last post by:
I'm trying to learn about introspection in Python. my ultimate goal is to be able to build a module "text database" of all modules that are in the sys.path, by discovering all candidate modules...
0
by: Steven T. Hatton | last post by:
I suspect the core language is not the level at which introspection should be implemented in C++. That has been the choice of C#, and Java. Both of these languages made some trade-offs to...
4
by: Steven T. Hatton | last post by:
Has there been any substantial progress toward supporting introspection/reflection in C++? I don't intend to mean it should be part of the Standard. It would, nonetheless, be nice to have a...
1
by: James Geurts | last post by:
Hi, Can someone tell me how to test if a field is a const? I am using the FxCop introspection engine, but I suppose I could use reflection if required. for example, the following would return...
2
by: greg.corson | last post by:
Hi, I'm in the process of building a complex system using a sort of "system services" model. I'm looking at putting together various services like serialization, networking, 2D/3D rendering,...
8
by: R. Bernstein | last post by:
In doing the extension to the python debugger which I have here: http://sourceforge.net/project/showfiles.php?group_id=61395&package_id=175827 I came across one little thing that it would be nice...
3
by: James Stroud | last post by:
Hello, I wanted to automagically generate an instance of a class from a dictionary--which might be generated from yaml or json. I came up with this: # automagical constructor def...
14
by: Dave Rahardja | last post by:
Is there a way to generate a series of statements based on the data members of a structure at compile time? I have a function that reverses the endianness of any data structure: /// Reverse...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.