simple integer subclass

Andreas Pfrengle a.pfrengle at
Tue Aug 3 01:52:25 CEST 2010

I'm trying to define a subclass of int called int1. An int1-object
shall behave exactly like an int-object, with the only difference that
the displayed value shall be value + 1 (it will be used to display
array indices starting at 1 instead of 0). Right now I have:

class int1(int):
    def __str__(self):
        return int.__str__(self + 1)

However, if I calculate with int1 and int- (or other number) objects,
the result is always coerced to an int (or other number object), e.g:
a = int1(5)
b = 5
print a      # "6"
print a+b  #"10"

How can I tell int1 to be the "default integer object"? Do I need to
overload *every* mathematical operation method of int, or is there an
easier way?

More information about the Python-list mailing list