-2

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?

Yashu Seth
  • 935
  • 4
  • 9
  • 24
  • 1
    What's wrong with the type checks? – Alex Feb 24 '16 at 21:07
  • They will cause unnecessary overhead in a complex case when there are many unwanted checks but I am sure that the input is valid. – Yashu Seth Feb 24 '16 at 21:08
  • 1
    I guess you *could* add a third, optional parameter. If you don't want the error checks, call it with True, but the default is False? – zondo Feb 24 '16 at 21:09
  • @YashuSeth in the first case, what do you want your function to do if the input are not int or float? – njzk2 Feb 24 '16 at 21:09
  • @njzk2 raise a value error sorry I did not mention it. – Yashu Seth Feb 24 '16 at 21:11
  • You are tied to: 1. add another argument and condition as @zondo said; 2. declare two different functions (the typechecker calls the internal); 3. Use just one function and evaluate a+b+0 (although this could cause a false positive if a and b are compatible objects by `__add__` or `__radd__`, and the result of the operation is compatible with 0 by `__add__`). – Luis Masuelli Feb 24 '16 at 21:19
  • 3
    @YashuSeth what is wrong with not making the checks, and having the `+` raise an error on its own? – njzk2 Feb 24 '16 at 21:25
  • @njzk2 `+` won't raise when used with strings or lists. – Stop harming Monica Feb 24 '16 at 21:40
  • @njzk2 Because this is a simple example what happens when the function is complex and there are a number of type checks? – Yashu Seth Feb 24 '16 at 21:41

2 Answers2

3

One way could be to have to functions that you use depending on the use case, in case you can't rely on the try/catch pattern due to other reasons (but I would suggest it if you can):

def add_check(a, b):
    if isinstance(a, (float, int)) and isinstance(b, (float, int)):
        return add(a, b)

def add(a, b):
    return (a+b)

If you just want one function you'll have to tell it somehow when to do the checks and when not to, perhaps with an additional parameter:

def add(a, b, type_check=False):
    if type_check:
        if not isinstance(a, (float, int)) or not isinstance(b, (float, int)):
            raise TypeError("Invalid Parameters")
    return add(a, b)
olofom
  • 6,233
  • 11
  • 37
  • 50
  • That is the whole point. How can I avoid writing the same function twice? – Yashu Seth Feb 24 '16 at 21:13
  • @YashuSeth if you want only one function you'll have to tell it if it should do the additional check or not in some way. I've updated the answer with such example. – olofom Feb 24 '16 at 21:19
  • @oloform I get it and now I think this is the only better way to do it. – Yashu Seth Feb 24 '16 at 21:21
  • With the three-parameters version you always have to pass an additional parameter and check its value. That carries some overhead too and I am not sure it is better than what you had before. I didn't timeit anyway. – Stop harming Monica Feb 24 '16 at 21:38
1

Leave the function as is, use just + when you do not need or want type checking.

i) When a user calls it and might give input other than float or real.

Use c = add(a, b)

ii) When the function is used internally (many times) and I am completely sure that the input parameters would only be int or float.

Use c = a + b

You only need to write one function and do not incurre in more overhead than is strictly necessary.

Stop harming Monica
  • 12,141
  • 1
  • 36
  • 56