On Thu, Feb 18, 2010 at 8:19 AM, Andrey Fedorov <span dir="ltr"><<a href="mailto:anfedorov@gmail.com">anfedorov@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

It seems intuitive to me that the magic methods for overriding the +, -, <, ==, >, etc. operators should have no sideffects on their operands. Also, that == should be commutative and transitive, that > and < should be transitive, and anti-commutative.<div>




<br></div><div>Is this intuition written up in a PEP, or assumed to follow from the mathematical meanings?</div></blockquote><div><br></div><div>It may be intuitive to you, but its not true, written down anywhere, nor assumed by the language, and the mathematical meaning of the operators doesn't matter to Python. Python purposefully does not enforce anything for these methods. Consider:</div>

<div><br></div><div><div>>>> class Test(object):</div><div>...     def __init__(self, v):</div><div>...             self.v = v</div><div>...     def __add__(self, other):</div><div>...             self.v = self.v + other</div>

<div>...             return "Ow!"</div><div>... </div><div>>>> t = Test(5)</div><div>>>> t + 2</div><div>'Ow!'</div><div>>>> t.v</div><div>7</div><div><br></div></div></div>It not only alters an operand, but its not even returning a meaningful result. This can be abused, but is also useful for certain uses.<div>

<br clear="all"><div name="mailplane_signature">--S</div></div>