I have a function which has a parameter intTup. That function is being called by a user and hence the input might be erroneous, so I want to validate it.
In particular, intTup must :
- be a tuple
- be of length 3
- contain only ints
So, as soon as the function is executed, those properties are checked :
def myFunc(intTup) if type(intTup) is not tuple: print("intTup = ", intTup) print("type(intTup) = ", type(intTup)) raise Exception('In parameter intTup : should be a tuple of float') elif (len(h) != 3) : print("intTup = ", intTup) print("len(intTup) = ", len(intTup)) raise Exception('In parameter intTup : should contain three atoms') elif [int]*len(intTup) == map(type,intTup): print("intTup = ", intTup) print("type(intTup) = ", map(type,intTup)) raise Exception('In parameter h : all atoms should be ints') # Some actual code follow these tests Of course that type of checks is frequently encountered : anytime a function admits a parameter of type tuple containing n values of type t a similar check must be run. Hence, should I define a function checkTuple(tupleToTest, shouldBeLen, shouldBeType) to automate these checks ? Or should I leave how it is ? How do you proceed in your scripts ?
if type(intTup) is not tuple:insteadif not type(intTup) is tuple:?