The function multiply_by_ten takes a numeric argument, multiplies it by ten and returns the result back.
Before this function performs its multiplication it checks if the argument is a numeric. If the argument is not numeric, the function prints out the message notifying that the argument is not a digit and returns None.
Question. Some developers believe that any given function should be returning the same type of value regardless of the circumstances. So, if I would follow a such opinion then this function should not be returning None. How to handle a situation like this? Should the argument be checked before it is being sent to a function? Why?
def multiply_by_ten(arg):
if not str(arg).isdigit():
print 'arg is not a digit'
return
return float(arg) * 10
result = multiply_by_ten('abc')
1.03or3jor instances of thefractions.Fractionclass or so on.float(arg)is not good if the argument happens to be aFraction. For example see what happens if you doFraction(float(Fraction(1,3)))orFraction(float(Fraction(1,3))*10).