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 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.
>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)
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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) .
|
by: hasho |
last post by:
Why is "call by address" faster than "call by value"?
|
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...
|
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...
|
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...
|
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?
|
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...
|
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...
|
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...
|
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?
...
|
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"....
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
| | |