Suppose I have the following function -
def add(a, b):
if isinstance(a, (float,int)) and isinstance(b, (float,int)):
return (a+b)
else:
raise TypeError("Invalid Parameters")
Now this function is used in two cases -
i) When a user calls it and might give input other than float or real.
ii) When the function is used internally (many times) and I am completely sure that the input parameters would only be int or float.
How can I avoid the useless type checks when using the function internally?
Alternatively, What is the best way of writing, the above function and the following function as a single function?
def add(a, b):
return (a+b)
The above function is simple but there may be a case when there are number of checks and the function is quite large. In such a case, how can I avoid writing the same function twice?