3 ms·
Care to elaborate? Where is the complexity hidden?
by cl3misch 1y ago
Care to elaborate? Where is the complexity hidden?
- marvy 1y agothe full story is in the language reference, but the short answer is: __radd__ (unless it's more complicated than I realize)
- pansa2 1y agoYes, the next level of complexity is that `a + b` will sometimes fall back to `b.__radd__(a)` if `a.__add__(b)` returns `NotImplemented`. But also: - There are situations where `__radd__` takes priority over `__add__`. The rules for determining that priority are complex (and IIRC subtly different from the rules that determine whether `a < b` prioritises `a.__lt__(b)` or `b.__gt__(a)`). - The lookup of `__add__` etc uses a special form of attribute lookup that's neither equivalent to `a.__add__` nor `type(a).__add__`. This special lookup only searches `type(a)` whereas the first would find an `__add__` function on `a`, and the second on `type(type(a))`. I've also heard of further complications caused by implementation details leaking into the language semantics - for example, see Armin Ronacher's blog post: https://lucumr.pocoo.org/2014/8/16/the-python-i-would-like-to-see/ https://lucumr.pocoo.org/2014/8/16/the-python-i-would-like-t...
- d0mine 1y ago1. operators use special lookup for dunder methods--on class, not on instance: >>> a+1 # lookup on class 1+1 2 >>> a.__add__(1) # instance method 0 2. There is __radd__ that is called if __add__ doesn't support given types (for different types).