Python's parser is deliberately very simple - one of the constraints it enforces on itself is that, to figure out what a token means, it can only look one token to the right (it is an LL(1) parser).
So, it sees [number][dot], and determines that it is a floating point literal. '_'
isn't a valid character to have in a floating point literal, so it gives a syntax error.
The most obvious and most common way to overcome this is to put the number in parentheses:
(1).__add__(2)
This forces it to interpret the 1
as an integer literal, and the dot as attribute access, within the limitations of the parser.
Another interesting workaround is this:
>>> 1 .__add__(2)
3
That is, add a space before the .
. It turns out that Python always allows a space there for any attribute lookup:
>>> range(4) .count(3)
1
I found this quite surprising, but it seems that Python treats .
under similar rules to +
, and so will allow as much space as you like around it.