Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 559
poitroae Avatar asked Aug 19 '12 09:08

poitroae


1 Answers

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.

like image 116
lvc Avatar answered Sep 21 '22 15:09

lvc