<div>On Mon, Jun 28, 2010 at 1:13 PM, Emile van Sebille <span dir="ltr"><<a href="mailto:emile@fenx.com">emile@fenx.com</a>></span> wrote:</div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
On 6/28/2010 12:02 PM Benjamin Kaplan said...<div class="im"><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Just to save the OP some trouble later on: this optimization is done<br>
for most of the __*__ methods. Overriding __add__ on an instance won't<br>
change the behavior of a + b.<br>
</blockquote>
<br></div>
ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on<br>
Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)] on win32<br>
Type "help", "copyright", "credits" or "license" for more information.<br>
>>> class Test: pass<br>
...<br>
>>> i = Test()<br>
>>> i+1<br>
Traceback (most recent call last):<br>
File "<stdin>", line 1, in ?<br>
TypeError: unsupported operand type(s) for +: 'instance' and 'int'<br>
>>> def __add__(self): return 6<br>
...<br>
>>> i.__add__ = __add__<br>
>>> i+1<br>
6<br>
>>><br>
<br>
Was this in reference to a specific python version?<br></blockquote><div><br></div><div>That limitation only applies to new-style classes. (The stuff below is from Python 2.6x86 on Windows, but should apply to your 2.4.1 as well).</div>
<div><br></div><div>>>> class Test(object): pass</div><div>...</div><div>>>> i = Test()</div><div>>>> i + 1</div><div>Traceback (most recent call last):</div><div> File "<stdin-inspect>", line 1, in <module></div>
<div>TypeError: unsupported operand type(s) for +: 'Test' and 'int'</div><div>>>> def __add__(self): return 6</div><div>...</div><div>>>> i.__add__ = __add__</div><div>>>> i + 1</div>
<div>Traceback (most recent call last):</div><div> File "<stdin-inspect>", line 1, in <module></div><div>TypeError: unsupported operand type(s) for +: 'Test' and 'int'</div><div>>>></div>
<div><br></div><div>Chris</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><font color="#888888"><br>
Emile</font><div><div></div><div class="h5"><br>
<br>
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>