I'm still fairly new to Python and I'm trying to get used to its dynamic typing. Sometimes I have a function or a class that expects a parameter of a certain type, but could get a value of another type that's coercible to it. For example, it might expect a float
but instead receive an int or a decimal. Or it might expect a string, but instead receive an object that defines the __str__
special method.
What is the best practice for coercing the argument to the right type (and the reason for it)? Do I do it in the function/class or in the caller? If in the caller, do I also check for it in the function? Eg.
Alternative 1:
def myfunc(takes_float):
myval = float(takes_float)
myfunc(5)
Alternative 2:
def myfunc(takes_float):
myval = takes_float
myfunc(float(5))
Alternative 3:
def myfunc(takes_float):
assert isinstance(takes_float, float)
myval = takes_float
myfunc(float(5))
I've already read this answer and this one and they say that checking types in Python is "bad", but I don't want to waste time tracking down very simple bugs which would be instantly picked up by the compiler in a statically typed language.