SyntaxError not honoured in list comprehension?

Carl Banks pavlovevidence at gmail.com
Sun Jul 4 06:35:31 EDT 2010


On Jul 4, 1:31 am, jmfauth <wxjmfa... at gmail.com> wrote:
> Python all versions.
>
> It's not a bug, but I'm suprised the following does
> not raise a SyntaxError (missing space between
> '9' and 'for').
>
> >>> [9for c in 'abc']
> [9, 9, 9]

It does seem strange that Python's lexer wouldn't consider 9for as a
single token.  Even tough it's not a valid token in Python, your eye
kind of sees it as one, so wouldn't it be better to raise a syntax
error?

Some other places were keyword can follow a number:

9if 0 else 1  (but not "9if 0else 1")
9and 0
9or 0
9in (1,2,3)
9is None


> Side effect: If this behaviour is considered as correct,
> it makes a correct Python code styling (IDLE, editors, ...)
> practically impossible to realise.

I'm not sure why an odd corner of the grammar would mess the whole
thing up.  Most code stylers only approximate the actual grammar
anyway.


Carl Banks



More information about the Python-list mailing list