473,287 Members | 1,399 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,287 software developers and data experts.

call f(a, *b) with f(*a, **b) ?

This question seems easy but I can't figure it out.
Lets say there's a function:

def f(a, *args):
print a
for b in args: print b

and elsewhere in your program you have a list and a dict like this:
args = [2, 3]
kwargs = {'a':1}

I'd like to get f() to print something like the following, but I can't
figure out how.
1
2
Jun 27 '08 #1
5 1009

"bukzor" <wo**********@gmail.comwrote in message
news:55**********************************@2g2000hs n.googlegroups.com...
This question seems easy but I can't figure it out.
Lets say there's a function:

def f(a, *args):
print a
for b in args: print b

and elsewhere in your program you have a list and a dict like this:
args = [2, 3]
kwargs = {'a':1}

I'd like to get f() to print something like the following, but I can't
figure out how.
1
2
I think there's no 'standard' way to do this. but:

import inspect
f(*map(kwargs.get, inspect.getargspec(f)[0])+args)

i don't know if it works because i'm afraid to try it. if it doesn't the
solution is something similar to that.
Jun 27 '08 #2
>1
>2
actually, you don't want it to print 3 also? if not, then you would do
f(*map(kwargs.get, inspect.getargspec(f)[0])+args[:1])
import inspect
f(*map(kwargs.get, inspect.getargspec(f)[0])+args)

Jun 27 '08 #3
On May 22, 5:39*pm, "inhahe" <inh...@gmail.comwrote:
1
2

actually, you don't want it to print 3 also? *if not, then you would do
f(*map(kwargs.get, inspect.getargspec(f)[0])+args[:1])
import inspect
f(*map(kwargs.get, inspect.getargspec(f)[0])+args)

No, that was a typo. Thanks tho.
Jun 27 '08 #4
On May 22, 5:29*pm, "inhahe" <inh...@gmail.comwrote:
"bukzor" <workithar...@gmail.comwrote in message

news:55**********************************@2g2000hs n.googlegroups.com...
This question seems easy but I can't figure it out.
Lets say there's a function:
def f(a, *args):
* *print a
* *for b in args: print b
and elsewhere in your program you have a list and a dict like this:
args = [2, 3]
kwargs = {'a':1}
I'd like to get f() to print something like the following, but I can't
figure out how.
1
2

I think there's no 'standard' way to do this. but:

import inspect
f(*map(kwargs.get, inspect.getargspec(f)[0])+args)

i don't know if it works because i'm afraid to try it. *if it doesn't the
solution is something similar to that.
That does, in fact work. Thanks! I'm a little sad that there's no
builtin way to do it, owell.
>>def f(a, *args):
... print a
... for b in args: print b
...
>>import inspect
a = [2,3]
b = {'a':1}
inspect.getargspec(f)
(['a'], 'args', None, None)
>>map(b.get, inspect.getargspec(f)[0])
[1]
>>map(b.get, inspect.getargspec(f)[0]) + a
[1, 2, 3]
>>f(*map(b.get, inspect.getargspec(f)[0]) + a)
1
2
3
Jun 27 '08 #5
bukzor <wo**********@gmail.comwrote:
That does, in fact work. Thanks! I'm a little sad that there's no
builtin way to do it, owell.
>def f(a, *args):
... print a
... for b in args: print b
...
>import inspect
a = [2,3]
b = {'a':1}
inspect.getargspec(f)
(['a'], 'args', None, None)
>map(b.get, inspect.getargspec(f)[0])
[1]
>map(b.get, inspect.getargspec(f)[0]) + a
[1, 2, 3]
>f(*map(b.get, inspect.getargspec(f)[0]) + a)
1
2
3
If I saw that in my code I'd be wanting to get rid of it as soon as
possible!

I'd re-write f() to have all named arguments then the problem becomes
easy and the answer pythonic (simple dictionary manipulation)...

So instead of f(a, *args) have f(a, list_of_args).

The f(*args) syntax is tempting to use for a function which takes a
variable number of arguments, but I usually find myself re-writing it
to take a list because of exactly these sort of problems. In fact I'd
be as bold to say that f(*args) is slightly un-pythonic and you should
avoid as a user interface. It does have its uses when writing
polymorphic code though.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #6

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

Similar topics

23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
4
by: John | last post by:
Hi all, This really is quite an urgent matter. I have a page with multiple, dynamically-loaded user controls and when a user clicks on a button, the whole form is submitted. Now at this stage...
5
by: Amaryllis | last post by:
I'm trying to call a CL which is located on our AS400 from a Windows application. I've tried to code it in different ways, but I seem to get the same error every time. Does anyone have any clue...
13
by: mitchellpal | last post by:
i am really having a hard time trying to differentiate the two..........i mean.....anyone got a better idea how each occurs?
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
3
by: cberthu | last post by:
Hi all, Is it possible to have two connects in the same rexx script to different DB's? I have to get data form on DB (with specifics selects and filter out some values with RExx) and save the...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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)...

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.