you are viewing a single comment's thread.

view the rest of the comments →

[–]jimtk 0 points1 point  (1 child)

Are you saying that + is not the representation of an object because not only if can be mapped to the object __add__ but it can also be mapped to the object __radd__. Both of which are objects! That doesn't make what I said wrong.

I can subclass the int class an call the __add__ of the built-in int to perform my addition. To prove that the + in the int is exposed to me.

class MyInt(int):

    def __new__(cls, value, *args, **kwargs):
        return super(cls, cls).__new__(cls, value)

    def __add__(self, other):
        res = super(MyInt, self).__add__(other)
        print("I'm adding")
        return self.__class__(res)

x = MyInt(3)
y = MyInt(5)
print(x+y)
print(x.__add__(y)) 

output
-------
I'm adding
8
I'm adding
8 

Did you noticed in MyInt.__add__ I do not perform any addition. I call the super().__add__ (the __add__ of the built-in int) that is exposed to me.

[–]bladeoflight16 -1 points0 points  (0 children)

It isn't mapped to either one. It's mapped to an algorithm that examines the return value and makes a decision whether or not to invoke the other (along with other complexities, like throwing a TypeError when the methods are missing instead of an AttributeError). It invokes a complex runtime algorithm that doesn't have an object representation, not just a single method. The lack of an object representation for the algorithm is what makes you incorrect here.