inconsistency with += between different types ?
Delaney, Timothy
tdelaney at avaya.com
Fri Aug 9 02:55:13 EDT 2002
> From: tanzer at swing.co.at [mailto:tanzer at swing.co.at]
>
> Still doesn't work:
>
> >>> class Int (int) :
> ... def __add__(self, other):
> ... print "Int.__add__"
> ... int.__add__(self,other)
> ... return self
> ... __radd__ = __add__
> ... __iadd__ = __add__
> ...
> >>> a = Int(5)
> >>> b = Int(7)
> >>> c = a + b
> Int.__add__
> >>> c
> 5
> >>> c+=7
> Int.__add__
> >>> c
> 5
Damned off-the-cuff, untested code ;) I of course did a mutable __add__ -
which is a *bad* thing ... but a completely separate bit of confusion ...
class Int:
def __init__(self, value):
self.value = value
def __add__(self, other):
print '__add__: %s, %s' % (self.value, other,)
return self.value + other
__radd__ = __add__
__iadd__ = __add__
a = Int(5)
b = Int(7)
c = a + b
print c
a += 7
print a
---------- Run ----------
__add__: 5, <__main__.Int instance at 00829F04>
__add__: 7, 5
12
__add__: 5, 7
12
Tim Delaney
More information about the Python-list
mailing list