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

Unpacking sequences and keywords in one function call

ram
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

Nov 14 '06 #1
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
Nov 14 '06 #2
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
Nov 14 '06 #3
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

Nov 14 '06 #4
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
Nov 14 '06 #5
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
Nov 14 '06 #6

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

Similar topics

2
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:...
2
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...
4
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...
39
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. ...
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...
7
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...
5
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...
20
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++);
1
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{
0
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...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
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...

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.