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 7 1034
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
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
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
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)
"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!
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 ?
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |