subclass of integers

Zentrader zentraders at gmail.com
Fri Sep 14 10:49:11 EDT 2007


I would do something along the lines of the following, although it
only tests for integers and not floats, so would return 'None' for a
float.

class Nint(int):
    def __add__(self, x, y):
        if isinstance(x, int) and isinstance(y, int):
            return x+y
        return None

if __name__=='__main__':
    N=Nint()
    print N.__add__( 1, 2 )
    print N.__add__( 1, None )




More information about the Python-list mailing list