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 1451
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: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
| |