simple integer subclass
samwyse at gmail.com
Tue Aug 3 02:24:51 CEST 2010
On Aug 2, 6:52 pm, Andreas Pfrengle <a.pfren... at gmail.com> wrote:
> 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?
I had a similar problem a few years ago, and couldn't find a solution
then. The thread from back then may shed some light on your problem.
More information about the Python-list