473,385 Members | 2,180 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,385 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 1016

"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: 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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.