A small inconsistency in syntax?
tjreedy at home.com
Thu Oct 25 18:56:32 CEST 2001
"Michael Abbott" <michael at rcp.co.uk> wrote in message
news:Xns91459674D7271michaelrcpcouk at 18.104.22.168...
> It seems to me that there's a little inconsistency in assigning to
There is no such thing as assigning to tuples or to any other type of
object. There is only assigning names (on the left side) to objects
(on the right side) as recorded in the local or global namespace.
> >>> (x,) = ()
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> ValueError: unpack tuple of wrong size
More clearly, "cannot assign name 'x' to non-existent first member of
> >>> () = ()
> SyntaxError: can't assign to ()
More clearly, "cannot assign object (only names) to object"
> Eh? Why not? It's just a matter of checking that the right hand
> really is an empty tuple...
No, its a matter of checking the the left hand side consists of legal
> I actually hit this in some code, where I wrote:
> time, status, () = myobject.read()
> where I happen to know that that in this particular case my object
> return an empty tuple in its third argument.
It should be clearer now why the third 'assignment' is meaningless and
does not work.
> Ok, so it's easy enough for me to write
> time, status, _ = myobject.read()
> instead, so I'm grumbling about trivia here.
The problem is that Python assignment is not sufficiently well
explained for many. It is quite different from assignment in
languages that assume a linear memory space with integer address in
which names represent chunks of that linear memory and in which
assignment 'to a name' represents assignment to the chunk of memory
designated by that name.
Terry J. Reedy
More information about the Python-list