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 1064
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_cod e.co_argcount
4
pydoit.func_cod e.co_varnames
('a', 'b', 'c', 'x')
pydoit.func_def aults
(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.getar gspec(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(somefun ction, 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_cod e.co_argcount
4
pydoit.func_cod e.co_varnames
('a', 'b', 'c', 'x')
pydoit.func_def aults
(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_co de.co_argcount 4 pydoit.func_co de.co_varnames ('a', 'b', 'c', 'x') pydoit.func_de faults (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_c ode.co_argcount 4 pydoit.func_c ode.co_varnames ('a', 'b', 'c', 'x') pydoit.func_d efaults (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 before. Thus,
a couple of questions which I was not able to solve with online research:
1. To send an arbitrary object to Flash, I would like to use
introspection to find out the values of all its instance variables. How
|
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 (I've
already done that), importing all of them, and then introspecting on
each module to discover its functions, globals and classes. But for
now I am having a problem with the latter.
I would like to import a module and figure out the names of...
|
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 accomplish this. Like threading,
introspection can probably be implemented by third party providers. I tend
to favor open standards that are acceptted and respected by a significant
portion of the developers working in the field. Stroustrup states the...
|
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 generally accepted means of providing
introspection.
My inclination is to have two general categories of introspection: dynamic
introspection, and static intospection. In situations where it makes sense
to use virtual functions and their...
|
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 true:
private const string DEFAULT_STRING = "Default";
thanks
| |
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, debugger-like class
inspection and other assorted stuff, but I need a way to bind them to
other people's C++ code at runtime using external scripting.
In particular I need a way to be able to access members of a class at
runtime by name, and I...
|
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 to get done better.
I notice on stack traces and tracebacks, an exec or execfile command
appears as a stack entry -- probably as it should since it creates a
new environment.
However the frame information for exec or execfile looks like this:...
|
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 construct(cls, adict):
dflts = cls.__init__.im_func.func_defaults
vnames = cls.__init__.im_func.func_code.co_varnames
|
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 the endianness of a data structure "in place".
template <typename T>
void reverseEndian(T&);
Using boost, it is possible to provide the default implementation for all POD
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |