Unpacking sequences and keywords in one function call

R

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
arg: 1
arg: 2
arg: 1
arg: 2
arg: 1
arg: 2
a = 1
File "<stdin>", line 1
f(*[1,2], a=1)
^
SyntaxError: invalid syntax

Thanks,
Rick
 
N

Noah Rawlins

ram said:
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
arg: 1
arg: 2
arg: 1
arg: 2
arg: 1
arg: 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...

noah
 
G

Gabriel Genellina

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
arg: 1
arg: 2
arg: 1
arg: 2
arg: 1
arg: 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
 
S

Steve Holden

Noah said:
ram said:
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
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
 
O

OKB (not okblacke)

Steve said:
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
 
D

Dennis Lee Bieber

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.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
O

OKB (not okblacke)

Dennis said:
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
 
D

Dennis Lee Bieber

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:
I may have missed an aspect of the thread... Howe'er, I could see it
a partial requirement of the "compile" phase too... Since anything
passed using */** has in indeterminate length during the compile pass,
the compile for the call may attempt to map all positionals 1:1 first,
before generating code to expand and map from the * parameter
a = [2,3]
f(1, *a, 4)

Parameter "4" would be stuck as a floating parameter, the compiler
can not map it to a positional argument, especially if "f" is defined
something like

def f(m, n, *p):

By your syntax, the compiler would have to unpack/repack

f(1, *[2, 3], 4)

into

f(1, 2, (3, 4) )

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top