For review: PEP 308 - If-then-else expression

James Kew james.kew at btinternet.com
Sat Feb 8 19:18:02 EST 2003


Paul Rubin wrote:
> Btw, just the other night I discovered that Python has a feature that
> I first saw in C: you can enter a list or tuple with an extra comma at
> the end (like [2,3,5,] , and the extra comma is ignored.  I think that
> feature was added to C for the sake of easing the job of automatically
> emitting tables for use in C programs, i.e. to simplify Yacc.

Quite so -- although annoyingly, the trailing-comma-is-allowed relaxation
doesn't apply to enum definitions. (At least not in C89; dunno about C99.)

Python _needs_ the trailing-comma loophole, though, as there's otherwise no
syntactically distinct way of expressing a one-element tuple...

>>> i = (1)
>>> i, type(i)
(1, <type 'int'>)
>>>
>>> t = (1,)
>>> t, type(t), len(t)
((1,), <type 'tuple'>, 1)

...at least without coming at it from the side:

>>> tuple([1])
(1,)

--
James Kew
james.kew at btinternet.com






More information about the Python-list mailing list