Should: "for k,v in **dictionary_instance" work?

irstas at gmail.com irstas at gmail.com
Sat Jun 16 03:20:37 EDT 2007


On Jun 16, 5:27 am, Steven D'Aprano
<s... at REMOVE.THIS.cybersource.com.au> wrote:
> Currently, *t and **d are syntax errors outside of function calls and
> definitions. (Any other places?) But if they were allowed, what would they
> mean?

Actually since you asked, I had to try this out

x = range(10)
a, *b = x

I would take it to mean a = x[0], b = x[1:] (or more precicely, it
would
probaby have to use the iter protocol). Unfortunately it fails..
Since these are roughly equivalent:

t = 1, (2, 3)

a, (b, c) = t

def foo(a, (b, c)): print a,b,c
foo(*t)

I would like to see the assignment enhanced so that this works too

t = 1, (2, 3), 4, 5

a, (b, c), *d = t

def foo(a, (b, c), *d): print a,b,c,d
foo(*t)

The latter one with function definition works fine, the former
assignment doesn't (SyntaxError). It's a bit surprising. Is there
any reason not to support this syntax? It would be neatly symmetric
in my opinion, so one wouldn't have to learn anything new. In fact,
this addition would reduce the amount of things one must learn
(unexpected behaviour).

def rawr((a, b, (c, *d)), (e, *f), **g): pass




More information about the Python-list mailing list