Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you do <code>a + b</code>, it calls call <code>self.value.__add__(other)</code>. Since <code>a.value</code> is 10, this calls <code>(10).__add__(b)</code>. Since <code>b</code> is an <code>adder</code> instance, the integer 10 does not know how to add itself to <code>b</code>, so it returns <code>NotImplemented</code>. The <code>__radd__</code> method of <code>b</code> is not called at this stage! Since you return the result of <code>self.value.__add__(other)</code>, your <code>__add__</code> method also returns NotImplemented. Once this happens, <code>b.__radd__(a)</code> is called, and also returns <code>NotImplemented</code> for the same reason --- it delegates to <code>(12).__radd__(a)</code>, which fails. Since both return <code>NotImplemented</code>, Python thinks the objects don't know how to add to each other, so it raises the error.</p> <p>The solution is, as you found, to use <code>+</code> instead of directly calling the magic methods. (You don't need to make a separate class for that; just change your <code>adder</code> class to call <code>self.value + other</code> instead of <code>self.value__add__(other)</code>, etc.) When you use <code>+</code>, you activate Python's internal machinery, whereby it calls <code>__add__</code> on the left operand and then, if that doesn't work, calls <code>__radd__</code> on the right operand. When you directly call <code>__add__</code> and <code>__radd__</code>, you are bypassing this internal machinery and only calling one of the two. This means that you never get to a state where both objects "reduce" themselves to their underlying values. Instead, you try the left-hand <code>__add__</code>, and when it fails, you don't "fall back" to <code>__radd__</code> --- you start again from the beginning with <code>__radd__</code>, losing the "value reduction" you did in the first part.</p> <p>The crucial thing is that <code>10 + b</code> knows to call <code>b.__radd__(10)</code> if <code>(10).__add__(b)</code> fails, but if you just directly call <code>(10).__add__(b)</code>, it doesn't know anything about <code>__radd__</code>, so <code>b.__radd__(10)</code> is never called.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload