问题
I was expecting __add__
to work, but that's obviously not the case (in Python 3.8.0 at least):
>>> 10 .__add__(5.5)
NotImplemented
>>> 10 .__radd__(5.5)
NotImplemented
The other way around works:
>>> 5.5 .__add__(10)
15.5
>>> 5.5 .__radd__(10)
15.5
Why doesn't the first code work? Is there a good reason behind it?
If those methods don't work, then what method is used by "syntactic sugar" operations such as 10 + 5.5
or 10 - 5.5
?
来源:https://stackoverflow.com/questions/59644166/adding-a-float-to-an-integer-using-a-method-not-an-operator