0

I'm asking about situations where if a wrong type of argument is passed to the function, it could:

  1. Blow up the whole thing.
  2. Return unexpected results
  3. Return nothing

For instance, the function below expects the argument name to be a string. It would throw an exception for all other types that doesn't have a startswith method.

def fruits(name):
    if name.startswith('O'):
        print('Is it Orange?')

There are other cases where a function could halt or cause damage to the system if execution proceeds without type-checking. Whenever there are a lot of functions or functions with a lot of arguments, type checking is tedious and makes the code unreadable. So, is there a standard for doing this? As to 'how to type check' - there are plenty of examples here on stackexchange, but I couldn't find any about where it would be appropriate to do so.

Another example would be:

def fruits(names):
    with open('important_file.txt', 'r+') as fil:
        for name in names:
            if name in fil:
                # Edit the file

Here if the name is a string each character in it will influence the editing of the file. If it is any other iterable, each element provided by it would influence the editing. Both of these could produce different results.

So, when should we type-check an argument and should we not?

2 Answers2

0

The answer off the top of my head would be: it depends where the input comes from.

If the functions are class methods that get invokes internally or things like that, you can assume the inputs are valid, because you wrote it!

For example

def add(x,y):
    return x + y

def multiply(a,b):
    product = 0
    for i in range(a):
        product = add(product, b)
    return product

In my add function, I could check that there is a + operator for the parameters x and y. But since I wrote the multiply function, and that is the only function that uses add, it is safe to assume the inputs will be int because that's how I wrote it. Now that argument stands on shaky ground for large code bases where you (hopefully) have shared code, so you can't be sure people don't misuse your functions. But that's why you comment them well to describe the correct use of said function.

If it has to read from a file, get user input, etc, then you may want to do some validation first.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Say, if an entire module is dedicated to doing just backend operations that are not directly used by the client, then we could ignore type-checking? –  Oct 05 '14 at 15:44
-1

I almost never do type checking in Python. In accordance with Pythonic philosophy I assume that me and other programmers are adult people capable of reading the code (or at least the documentation) and using it properly. I assume that we test our code before we let it destroy something important. After all in most cases if you do something wrong, you'll just see an error and Python's error messages are quite informative most of the time.

The only occasion when I sometimes check types is when I want my function to behave differently depending on the argument's type. But although I sometimes feel compelled to do this, I don't consider it a good practice.

Most often it happens when my function iterates over a list of strings and I fear (or want) I could get a single string passed into it by accident - this won't throw an error at once because unfortunately string is an iterable too.

Sventimir
  • 1,996
  • 3
  • 14
  • 25