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
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
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
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.
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
>
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
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
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
>
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
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__
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 (
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 !=
12 matches
Mail list logo