Below is the program written to perform the type check in addition to primary functionality(i.e., square root).
from math import sqrt
def isnumber(thing):
try:
int(thing)
except:
return False
return True
def make_safe(f, isnumber):
def type_check(datum):
if isnumber(datum):
return f(datum)
else:
return False
return type_check
safe_sqrt = make_safe(sqrt, isnumber)
print(safe_sqrt('hello'))
If an abstraction is designed for any functionality in dynamic language(like python), there is more chance of runtime type check failures compared to static-type language like java.
In fact python is more strictly typed in runtime compared to java at build time. Python has it's own type hierarchy.
So,
What advantage does a programmer have in using dynamic language when type checking is any ways getting deferred to runtime? Is it not crucial to take control of type checking by a programmer rather than interpreter?
Aucun commentaire:
Enregistrer un commentaire