Re: subclass of integers

2007-09-15 Thread Roberto Bonvallet
On Sep 14, 10:30 am, Mark Morss <[EMAIL PROTECTED]> wrote: > I would like to construct a class that includes both the integers and > None. I desire that if x and y are elements of this class, and both > are integers, then arithmetic operations between them, such as x+y, > return the same result as

Re: subclass of integers

2007-09-14 Thread Gabriel Genellina
En Fri, 14 Sep 2007 20:16:36 -0300, Dan Bishop <[EMAIL PROTECTED]> escribi�: > On Sep 14, 9:30 am, Mark Morss <[EMAIL PROTECTED]> wrote: >> I would like to construct a class that includes both the integers and >> None. I desire that if x and y are elements of this class, and both >> are integer

Re: subclass of integers

2007-09-14 Thread Dan Bishop
On Sep 14, 9:30 am, Mark Morss <[EMAIL PROTECTED]> wrote: > I would like to construct a class that includes both the integers and > None. I desire that if x and y are elements of this class, and both > are integers, then arithmetic operations between them, such as x+y, > return the same result as

Re: subclass of integers

2007-09-14 Thread Ian Clark
Ian Clark wrote: > Mark Morss wrote: >> I would like to construct a class that includes both the integers and >> None. I desire that if x and y are elements of this class, and both >> are integers, then arithmetic operations between them, such as x+y, >> return the same result as integer addition.

Re: subclass of integers

2007-09-14 Thread Ian Clark
if either x or y > is None, these operations return None. > > It's simple enough to construct a subclass of integers that behave in > this way: > > class Nint(int): > def __add__(self,other): > if (other != None): > return self+other >

Re: subclass of integers

2007-09-14 Thread Bruno Desthuilliers
However if either x or y > is None, these operations return None. > > It's simple enough to construct a subclass of integers that behave in > this way: > > class Nint(int): > def __add__(self,other): > if (other != None): > return self+oth

Re: subclass of integers

2007-09-14 Thread Bruno Desthuilliers
Zentrader a écrit : > This would accept ints, floats, and decimal types. It doesn't... > import decimal Useless > class Nint(int): > def __add__(self, x, y): The prototype for __add__ is __add__(self, other) > try: > return x+y > except: > return No

Re: subclass of integers

2007-09-14 Thread Michael Spencer
if either x or y > is None, these operations return None. > > It's simple enough to construct a subclass of integers that behave in > this way: > > class Nint(int): > def __add__(self,other): > if (other != None): > return self+other >

Re: subclass of integers

2007-09-14 Thread Zentrader
This would accept ints, floats, and decimal types. import decimal class Nint(int): def __add__(self, x, y): try: return x+y except: return None if __name__=='__main__': N=Nint() print N.__add__( 1, 2 ) print N.__add__( 1, None ) print N

Re: subclass of integers

2007-09-14 Thread Zentrader
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__

Re: subclass of integers

2007-09-14 Thread Mark Morss
rn the same result as integer addition. However if either x or y > is None, these operations return None. > > It's simple enough to construct a subclass of integers that behave in > this way: > > class Nint(int): > def __add__(self,other): > if (

subclass of integers

2007-09-14 Thread Mark Morss
operations return None. It's simple enough to construct a subclass of integers that behave in this way: class Nint(int): def __add__(self,other): if (other != None): return self+other else: return None def __radd__(self,other): if (other !=