How to avoid overflow errors

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Sep 14 20:14:47 EDT 2007


I thought that overflow errors would be a thing of the past now that 
Python automatically converts ints to longs as needed. Unfortunately, 
that is not the case.

>>> class MyInt(int):
...     pass
...
>>> MyInt(sys.maxint)
2147483647
>>> MyInt(sys.maxint+1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to int


How do I subclass int and/or long so that my class also auto-converts 
only when needed?



-- 
Steven.



More information about the Python-list mailing list