Why does 1.__add__(2) not work out? [duplicate]

我的梦境 提交于 2019-12-04 23:06:11

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.

Python interprets 1. as float so you have to add parens:

>>> (1).__add__(2)
3

or space:

>>> 1 .__add__(2)
3

If you want float value here just put 2 dots.

>>> 1..__add__(2) #float
3.0
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!