Stupid question #983098403:
I can't seem to pass an unpacked sequence and keyword arguments to a
function at the same time. What am I doing wrong?
def f(*args, **kw):
for a in args:
print 'arg:', a
for (k,v) in kw.iteritems():
print k, '=', v
>>f(1,2)
arg: 1
arg: 2
>>f(*[1,2])
arg: 1
arg: 2
>>f(1,2, a=1)
arg: 1
arg: 2
a = 1
>>f(*[1,2], a=1)
File "<stdin>", line 1
f(*[1,2], a=1)
^
SyntaxError: invalid syntax
Thanks,
Rick 5 1499
ram wrote:
Stupid question #983098403:
I can't seem to pass an unpacked sequence and keyword arguments to a
function at the same time. What am I doing wrong?
def f(*args, **kw):
for a in args:
print 'arg:', a
for (k,v) in kw.iteritems():
print k, '=', v
>>>f(1,2)
arg: 1
arg: 2
>>>f(*[1,2])
arg: 1
arg: 2
>>>f(1,2, a=1)
arg: 1
arg: 2
a = 1
>>>f(*[1,2], a=1)
File "<stdin>", line 1
f(*[1,2], a=1)
^
SyntaxError: invalid syntax
Thanks,
Rick
I don't know if it's because there's some potential ambiguity (that I'm
not seeing), but yeah, you just can't do that. This should work though...
>>f(*[1, 2], **{'a':1})
noah
At Monday 13/11/2006 22:06, ram wrote:
>Stupid question #983098403:
I can't seem to pass an unpacked sequence and keyword arguments to a function at the same time. What am I doing wrong?
def f(*args, **kw):
for a in args:
print 'arg:', a
for (k,v) in kw.iteritems():
print k, '=', v
>f(1,2)
arg: 1 arg: 2
>f(*[1,2])
arg: 1 arg: 2
>f(1,2, a=1)
arg: 1 arg: 2 a = 1
>f(*[1,2], a=1)
File "<stdin>", line 1
f(*[1,2], a=1)
^
SyntaxError: invalid syntax
Reverse the order:
f(a=1, *[1,2])
--
Gabriel Genellina
Softlab SRL
__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Noah Rawlins wrote:
ram wrote:
>Stupid question #983098403:
I can't seem to pass an unpacked sequence and keyword arguments to a function at the same time. What am I doing wrong?
def f(*args, **kw): for a in args: print 'arg:', a for (k,v) in kw.iteritems(): print k, '=', v
>>>>f(1,2)
arg: 1 arg: 2
>>>>f(*[1,2])
arg: 1 arg: 2
>>>>f(1,2, a=1)
arg: 1 arg: 2 a = 1
>>>>f(*[1,2], a=1)
File "<stdin>", line 1 f(*[1,2], a=1) ^ SyntaxError: invalid syntax
Thanks, Rick
I don't know if it's because there's some potential ambiguity (that I'm
not seeing), but yeah, you just can't do that. This should work though...
>>f(*[1, 2], **{'a':1})
noah
You should be ok as long as you use the following ordering of actual
arguments in a function call:
1. Positional arguments
2. Keyword arguments
3. * sequence
4. ** dict
In other words try (untested):
f(a=1, *[1, 2])
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
Steve Holden wrote:
Noah Rawlins wrote:
You should be ok as long as you use the following ordering of
actual arguments in a function call:
1. Positional arguments
2. Keyword arguments
3. * sequence
4. ** dict
I was just wondering about this yesterday. Is there a reason you
can only unpack a sequence at the END of the positional argument list?
--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
Dennis Lee Bieber wrote:
On Tue, 14 Nov 2006 05:45:57 GMT, "OKB (not okblacke)"
<br************@NObrenSPAMbarn.netdeclaimed the following in
comp.lang.python:
>> I was just wondering about this yesterday. Is there a reason you can only unpack a sequence at the END of the positional argument list?
Not a reasoned explanation, but... Python has to be able to
assign
"defined" parameters before gathering the */** parameters.
Sorry, I don't really understand what you mean by that. I'm
talking about the UNpacking of a sequence into separate function
arguments, which I was assuming happened on the "outside" at the time
the function was called. That is, why aren't these two calls exactly
the same:
a = [2,3]
f(1, *a, 4)
f(1, 2, 3, 4)
I don't understand why Python would need to assign "defined
parameters" first -- the sequence unpacking here can happen before
anything is mapped to the function parameters at all. (Can't it?)
I can see why moving the kwargs up earlier in the list isn't valid,
because it isn't valid for normal argument-passing either; switching the
order of individually named kwargs and a **dict isn't meaningful because
the kwargs aren't ordered anyway. But is there a reason you can't
unpack a sequence into the middle of the positional argument list?
--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: bwooster47 |
last post by:
I'm a newcomer to python - what is the best way to convert a list into
a function call agruments?
For example:
list = (2005, 5, 5)
date = datetime.date( list )
fails with:
TypeError:...
|
by: Chris Michael |
last post by:
Hello everybody,
Newbie here. I've been working on this for the last two days and I can't
figure out where this problem is. I think it's something so obvious, but I
can't see it!
OK, firstly...
|
by: mangi03 |
last post by:
Hi,
I came acrosss g++ compile errors whenever I make a function call by
reference and found out from the test program that compiler is
treating the function argument differently when another...
|
by: Randell D. |
last post by:
Folks,
I'm sure this can be done legally, and not thru tricks of the trade - I
hope someone can help.
I'm writing a 'tool' (a function) which can be used generically in any
of my projects. ...
|
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: lordkain |
last post by:
Hello all,
I have a problem which I cant seem to solve, I dont know if it is
solvable.. it should though.
What I want is to call a function, whith x arguments on base of an xml
file. In the...
|
by: marcsirois |
last post by:
I have an SQL Query that looks like this. I simplified it for the
purpose of this example, but it's still a runnable query
SELECT TOP 1
FundedPositions.PositionNumber AS ,
(select top 1...
|
by: sam_cit |
last post by:
Hi Everyone,
I have the following code,
int main()
{
int p = {10,20,30,40,50};
int *i = &p;
printf("before : %p\n",(void*)i);
printf("1 %d %d\n",*++i,*i++);
|
by: George2 |
last post by:
Hello everyone,
Such code segment is used to check whether function call or exception-
handling mechanism runs out of memory first (written by Bjarne),
void perverted()
{
try{
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
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: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
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...
| |