Python operator overloading with multiple operands
<p>I know I can do simple operator overloading in python by the following way.</p>

<p>Let say overloading '+' operator.</p>

<pre><code>class A(object):
  def __init__(self,value):
    self.value = value

  def __add__(self,other):
    return self.value + other.value

a1 = A(10)
a2 = A(20)
print a1 + a2
</code></pre>

<p>But It fails when I try to do the following,</p>

<pre><code>a1 = A(10)
a2 = A(20)
a3 = A(30)
print a1 + a2 + a3
</code></pre>

<p>Since the <code>__add__</code> accepts only 2 parameters. What is the best solution to achieve operator overloading with n number of operands.</p>

<p>The problem is that</p>

<p><code>a1 + a2 + a3</code>
gives
<code>30 + a3</code></p>

<p>30 is an int, and ints do not know how to sum to A</p>

<p>You should return an instance of A in your <code>__add__</code> function</p>

python python-2.7 python-3.x python-2.x