A small inconsistency in syntax?

Tim Peters tim.one at home.com
Fri Oct 26 15:26:00 EDT 2001


[Steve Holden]
> This is getting TOO weird. Should this really be allowed? Does it make
> any kind of sense? The attached comes from 2.0, but 2.1.1 and 2.2b1 give
> the same result.
>
> >>> [a,b] = (1,2) # no surprises here
> >>> a
> 1
> >>> b
> 2
> >>> [a,b][1] = 23 # WHAT!?!?!?!?!?!?
> >>> b
> 2
>
> What does the interpreter actually DO with this statement, and why is it
> allowed? To what is the value 23 actually bound here?

23 is assigned to the slot at index 1 of the anonymous list created by the
list-builder [a, b].  Much the same as, e.g.,

>>> anonymous = [a, b]
>>> anonymous[1] = 23
>>> del anonymous
>>> b

It appears to be a challenge to dream up a good use for it, though <wink>.





More information about the Python-list mailing list