A small inconsistency in syntax?

Steve Holden sholden at holdenweb.com
Thu Oct 25 11:22:23 EDT 2001


"Michael Abbott" <michael at rcp.co.uk> wrote in message
news:Xns91459674D7271michaelrcpcouk at 194.238.50.13...
> It seems to me that there's a little inconsistency in assigning to tuples:
>
>     >>> (x,) = ()
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in ?
>     ValueError: unpack tuple of wrong size
>
> Yes, that's what I expect.
>
Indeed.

>     >>> () = ()
>     SyntaxError: can't assign to ()
>
> Eh?  Why not?  It's just a matter of checking that the right hand side
> really is an empty tuple...
>
Oh, no it isn't. The best parallel I can think of is writing

>>> 2 = 34
Traceback (SyntaxError: can't assign to literal

although in your case you are trying to assign to a constructor rather than
a literal
.
> 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 will
> return an empty tuple in its third argument.
>
> Ok, so it's easy enough for me to write
>
>     time, status, _ = myobject.read()
>
> instead, so I'm grumbling about trivia here.

Indeed :-)

Or, if you really don't want to do that, how about

    time, status = myobject.read()[:2]

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list