int/long unification hides bugs

Josiah Carlson jcarlson at uci.edu
Thu Oct 28 13:47:35 EDT 2004


tweedgeezer at hotmail.com (Jeremy Fincher) wrote:
> 
> kartick_vaddadi at yahoo.com (kartik) wrote in message news:<940ee3e8.0410250456.65445bbd at posting.google.com>...
> > there seems to be a serious problem with allowing numbers to grow in a
> > nearly unbounded manner, as int/long unification does: it hides bugs.
> 
> I think one important clarification needs to be made to this
> statement: it hides bugs in code that depends on the boundedness of
> integers, written before int/long unification.
> 
> The problem with int/long unification is that there is no simple
> pure-Python alternative for those of us who need a bounded integer
> type.  If our code depended on that raised OverflowError in order to
> ensure that our computations were bounded, we're left high and dry
> with ints and longs unified.  We must either drop down to C and write
> a bounded integer type, or we're stuck with code that no longer works.

And as others have said more than once, it is not so common that the
boundedness of one's integers falls on the 32 bit signed integer
boundary.  Ages and money were given as examples.


> I'm by no means claiming that int/long unification is bad, only that
> it leaves a hole in Python's toolbox where none existed before.  I
> challenge anyone here to write a pure-Python class that does bounded
> integer arithmetic, without basically reimplementing all of integer
> arithmetic in Python.

And what is so wrong with implementing all of integer arithmetic in
Python?  About all I can figure is speed, in which case, one could do
the following...

class BoundedInt(object):
    a = [0]
    def f(a):
        a[0] = 1
        return 1
    assert f(a)
    if a[0]:
        def __new__(cls, val, lower, upper):
            #body for creating new bounded int object...
            #only gets called if Python _is not run_ with -O option.
    else:
        def __new__(cls, val, lower, upper):
            #optimized version is a plain integer
            return val
    del f;del a #clean out the namespace
    #insert code for handling bounded integer arithmetic here

Which uses plain integers when Python is run with the -O option, but
bounds them during "debugging" without the -O option.

 - Josiah




More information about the Python-list mailing list