On Mon, Jun 28, 2010 at 1:13 PM, Emile van Sebille <em...@fenx.com> wrote:
> On 6/28/2010 12:02 PM Benjamin Kaplan said... > > Just to save the OP some trouble later on: this optimization is done >> for most of the __*__ methods. Overriding __add__ on an instance won't >> change the behavior of a + b. >> > > ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on > Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)] on > win32 > Type "help", "copyright", "credits" or "license" for more information. > >>> class Test: pass > ... > >>> i = Test() > >>> i+1 > Traceback (most recent call last): > File "<stdin>", line 1, in ? > TypeError: unsupported operand type(s) for +: 'instance' and 'int' > >>> def __add__(self): return 6 > ... > >>> i.__add__ = __add__ > >>> i+1 > 6 > >>> > > Was this in reference to a specific python version? > 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). >>> class Test(object): pass ... >>> i = Test() >>> i + 1 Traceback (most recent call last): File "<stdin-inspect>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'Test' and 'int' >>> def __add__(self): return 6 ... >>> i.__add__ = __add__ >>> i + 1 Traceback (most recent call last): File "<stdin-inspect>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'Test' and 'int' >>> Chris > > Emile > > > -- > http://mail.python.org/mailman/listinfo/python-list >
-- http://mail.python.org/mailman/listinfo/python-list