By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,270 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,270 IT Pros & Developers. It's quick & easy.

Unpacking sequences and keywords in one function call

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.