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

别等时光非礼了梦想. 提交于 2019-12-06 17:27:56

问题


Possible Duplicate:
accessing a python int literals methods

In Python, everything is an object.

But then again, why doesn't the following snippet work?

1.__add__(2)

However, this does work:

n = 1
n.__add__(2)

What is the difference between n and 1?

Isn't it a design failure that it doesn't work? For instance, it does work with string literals as well.

"one".__add__("two")

For comparison, it works well on other purely object oriented languages too.

Let's take a closer look at this compiling c# example:

Console.WriteLine(100.ToString());

Then again, what distinguishes Python from C# in the perspective of everything is an object?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/12025531/why-does-1-add-2-not-work-out

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