As it is in the book (online edition), this function doesn't work:
def safe_division(number, divisor,
ignore_overflow,
ignore_zero_division):
try:
return number / divisor
except OverflowError:
if ignore_overflow:
return 0
else:
raise
except ZeroDivisionError:
if ignore_zero_division:
return float('inf')
else:
raise
To work, we need to add 4 spaces in front of the first else:
def safe_division(number, divisor,
ignore_overflow,
ignore_zero_division):
try:
return number / divisor
except OverflowError:
if ignore_overflow:
return 0
else:
raise
except ZeroDivisionError:
if ignore_zero_division:
return float('inf')
else:
raise
Also, if ignore_zero_divison is True, I expected the function to return -inf if number < 0 and divisor = 0. This can be obtained by premultiplying float('inf') by number:
def safe_division(number, divisor,
ignore_overflow,
ignore_zero_division):
try:
return number / divisor
except OverflowError:
if ignore_overflow:
return 0
else:
raise
except ZeroDivisionError:
if ignore_zero_division:
return number * float('inf')
else:
raise
The latter issue (always inf and not -inf for negative numerator and zero denominator) arises a couple of paragraph later and can be fixed in the same way:
As it is in the book (online edition), this function doesn't work:
To work, we need to add 4 spaces in front of the first else:
Also, if ignore_zero_divison is True, I expected the function to return -inf if number < 0 and divisor = 0. This can be obtained by premultiplying float('inf') by number:
The latter issue (always inf and not -inf for negative numerator and zero denominator) arises a couple of paragraph later and can be fixed in the same way:
Apart from these minor issues, excellent book, very enjoyable to read!