473,398 Members | 2,165 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,398 software developers and data experts.

Calling a function with unknown arguments?

Rob
I can get a list of a function's arguments.
>>def bob(a, b):
.... return a+b
....
>>bob.func_code.co_varnames
('a', 'b')

Let's say that I also have a dictionary of possible arguments for this
function.
>>possible = {'a':10, 'b':5, 'c':-3}
How do I proceed to call bob(a=10, b=5) with this information?

Thanks!

Mar 7 '07 #1
4 1606
On 2007-03-07, Rob <cr*****@mit.eduwrote:
I can get a list of a function's arguments.
>>>def bob(a, b):
... return a+b
...
>>>bob.func_code.co_varnames
('a', 'b')

Let's say that I also have a dictionary of possible arguments for this
function.
>>>possible = {'a':10, 'b':5, 'c':-3}

How do I proceed to call bob(a=10, b=5) with this information?
bob(a=possible['a'],b=possible['b'])

--
Grant Edwards grante Yow! BRILL CREAM is
at CREAM O' WHEAT in another
visi.com DIMENSION...
Mar 7 '07 #2
En Wed, 07 Mar 2007 19:55:08 -0300, Rob <cr*****@mit.eduescribió:
I can get a list of a function's arguments.
>>>def bob(a, b):
... return a+b
...
>>>bob.func_code.co_varnames
('a', 'b')

Let's say that I also have a dictionary of possible arguments for this
function.
>>>possible = {'a':10, 'b':5, 'c':-3}

How do I proceed to call bob(a=10, b=5) with this information?
You're almost done. Notice that co_varnames includes local variables too;
only the first co_argcount are positional arguments.
If you only want to deal with positional arguments (no *args, no **kw):

pydef bob(a,b):
.... c = a+b
.... return c
....
pybob.func_code.co_varnames
('a', 'b', 'c')
pybob.func_code.co_argcount
2
pyargs = dict((name, possible[name]) for name in
bob.func_code.co_varnames[:bo
b.func_code.co_argcount] if name in possible)
pyargs
{'a': 10, 'b': 5}
pybob(**args)
15

It can handle missing arguments (if they have a default value, of course):

pydef bob(a, k=3):
.... return a+k
....
pyargs = dict((name, possible[name]) for name in
bob.func_code.co_varnames[:bo
b.func_code.co_argcount] if name in possible)
pyargs
{'a': 10}
pybob(**args)
13

--
Gabriel Genellina

Mar 7 '07 #3
On Mar 7, 10:55 pm, "Rob" <crow...@mit.eduwrote:
I can get a list of a function's arguments.>>def bob(a, b):

... return a+b
...>>bob.func_code.co_varnames

('a', 'b')

Let's say that I also have a dictionary of possible arguments for this
function.
>possible = {'a':10, 'b':5, 'c':-3}

How do I proceed to call bob(a=10, b=5) with this information?

Thanks!
You can do this with a list comprehension:
bob(*[possible[k] for k in bob2.func_code.co_varnames])

The LC creates a list of arg values in the same order as the var names
for the function. Putting a * before the list when you call bob()
turns that list into it's arguments.
Alternativly (and this may not be useful) you can call a function with
a dictionary of arguments:

def bob(**kwargs):
print kwargs

possible = {'a' : 10, 'b':5, 'c':-3}

>>bob(**possible)
{'a': 10, 'c': -3, 'b': 5}
>>>
Tom

Mar 7 '07 #4
Rob
You can do this with a list comprehension:
bob(*[possible[k] for k in bob2.func_code.co_varnames])

The LC creates a list of arg values in the same order as the var names
for the function. Putting a * before the list when you call bob()
turns that list into it's arguments.
Thanks Tom! This turns out to be exactly what I needed.

Mar 8 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael | last post by:
In the book 'Text Processing Python,' they present a function that I'm having a real hard time understanding. apply_each = lambda fns, args=: map(apply, fns, *len(fns)) I understand that the...
10
by: jim.brown | last post by:
Please refer me to the right place if this is the wrong place to post this question. I'm looking for an example of calling the Eigenvalue routines of JAMA from a C++ program. The documentation...
8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
9
by: Marek Lewczuk | last post by:
Hello, I'm moving out from MySQL to PostgreSQL and there are some function which are not supported in PG so I'm trying to write my own functions. Currently I have big problem with function IF(),...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
2
by: ramasubramanian.rahul | last post by:
hi i am trying to call some java APIs from c . i use the standatd JNI calls to load the JVM from a c program and call all java functions by using a pointer to the jvm which was returned by the JNI...
2
by: Spoon | last post by:
Hello everyone, AFAIU, in C89, calling fprintf with too few arguments leads to UB. 4.9.6.1 The fprintf function Synopsis #include <stdio.h> int fprintf(FILE *stream, const char *format,...
12
by: tom_kuehnert | last post by:
Hi! I'm trying to execute a program using system(). The program itself is located in some path which might contain whitespaces. Simple solution would be this: system("\"C:\A B\C.exe\""); ...
6
by: Ole Nielsby | last post by:
VC has a __cdecl specifier which allows functions and methods to be called with varying parameter count. (I understand this is the default for functions in general but in VC, instances use...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.