Message253046
__floordiv__ in module fraction fails with TypeError instead of returning NotImplemented when modulo is a class without __rtruediv__ or __mul__.
Code sample:
class Foo(object):
def __rdivmod__(self, other):
return 'rdivmod works'
from fractions import Fraction
a = Fraction(1,1)
b = Foo()
print(divmod(1, b))
print(divmod(a, b))
__divmod__ in Fraction is inherited from class Real (numbers.py):
def __divmod__(self, other):
return (self // other, self % other)
So __floordiv__ and __mod__ are called.
def __floordiv__(a, b):
"""a // b"""
return math.floor(a / b)
def __mod__(a, b):
"""a % b"""
div = a // b
return a - b * div
__floordiv__ if fractions.py makes a true division, and __mod__ makes multiplication.
The following code will fix the problem:
def __divmod__(self, other):
if isinstance(a, numbers.Complex):
return (self // other, self % other)
else:
return NotImplemented |
|
Date |
User |
Action |
Args |
2015-10-15 14:09:54 | ShashkovS | set | recipients:
+ ShashkovS |
2015-10-15 14:09:54 | ShashkovS | set | messageid: <1444918194.04.0.71189202918.issue25412@psf.upfronthosting.co.za> |
2015-10-15 14:09:54 | ShashkovS | link | issue25412 messages |
2015-10-15 14:09:53 | ShashkovS | create | |
|