[Python-ideas] Allow attribute references for decimalinteger

MRAB python at mrabarnett.plus.com
Thu Oct 31 02:32:18 CET 2013


On 31/10/2013 01:01, אלעזר wrote:
> There's an unnecessary corner case regarding integer literals and attributes:
>>>> 1..real
> 1.0
>>>> 1.0.real
> 1.0
>>>> 1..real
> 1.0
>>>> 1. .real
> 1.0
>>>> .1.real
> 0.1
>>>> 1 .real
> 1
>>>> (1).real
> 1
>>>> 1.real
>    File "<stdin>", line 1
>      1.real
>           ^
> SyntaxError: invalid syntax
>
> Why does it fail? To my human eyes it seems (almost) completely
> unambiguous. Is it a lexing thing? I couldn't find an explanation (or
> any reference at all, although there should be) in the docs.
> The only ambiguity I can see is 1.j - but the desired meaning is
> clear: 1j (just like 1.0j).
>
It'll make the lexer more complicated if it can't tell whether the "."
is part of a float literal or not, and, anyway, it's already the case
that 1.j == 1.0j, not (1).j, so saying that 1.real == (1).real would be
inconsistent.

> It may confuse beginners; it made me believe that there's no such
> thing as 1.__class__ a couple of years ago. I guess it's bad for code
> generators too.
>
> I suggest making "1.identifier" legal, and adding `j` and `J`
> properties to numbers.Number to mean the sensible thing (so 0.j is not
> a special syntax as it today).
>
I'd prefer it if Python insisted that a decimal point be preceded and
followed by a digit, but changing it might break existing code. It's
one of those changes that could've been made in Python 3, I suppose,
but it's not something I'm losing any sleep over! :-)


More information about the Python-ideas mailing list