inconsistency with += between different types ?

Donn Cave donn at
Wed Aug 7 15:27:02 EDT 2002

Quoth Andreas.Leitgeb at (Andreas Leitgeb):
| To Christopher and other defendants of status quo:
|  Do you know of any real use that a not-self-mutating __ixxx__ 
|  may have, which its non-i version __xxx__ could not do ?
|  (I do *not* intend to argue that just because you perhaps cannot think 
|   of any at the moment, that the suggestion would necessarily be good,   
|   but if instead you happen to know a good use then we could instantly 
|   short-cut the discussion.)

Consider essentially numeric objects.  Wouldn't you want them to
act like builtin numberic types?

class Currency:
    def __init__(self, w, f):
        if type(w) != type(0) or type(f) != type(0):
            raise ValueError, 'ints only'
        self.unit = w
        self.pennies = f
    def __iadd__(self, v):
        return self + v
    def __add__(self, v):
        if type(v) == type(0):
            return self.__class__(self.unit + v, self.pennies)
        elif type(v) == type(self):
            return self.__class__(self.unit + v.unit, self.pennies + v.pennies)
            raise ValueError, 'int or currency'
    def __str__(self):
        return '%d.%02d' % (self.unit, self.pennies)

d = Currency(2, 25)
c = d
d += 2
print c, d

I don't know which I'd prefer.  I don't think the in place op
feature is useful enough to justify its flaws, period.

	Donn Cave, donn at

More information about the Python-list mailing list