Python: __add__ and +, different behavior with float and integer

前端 未结 1 1821
离开以前
离开以前 2021-01-12 16:58

When adding an integer value to a float value, I realized that __add__ method is working fine if called on float, such as this:

>>> n =         


        
1条回答
  •  梦毁少年i
    2021-01-12 17:32

    a + b does not directly translate to a.__add__(b). It also tries b.__radd__(a) if a.__add__ doesn't exist or returns NotImplemented, or if b is an instance of a subtype of a's type.

    0 讨论(0)
提交回复
热议问题