Referencing this question: What's the canonical way to check for type in Python?
It is said that the best way to check for inputs is to not check them - that is to let try/except blocks take care of bad inputs.
My question is that if I want to design a function that handles multiple inputs, my intuition is to do something like this
def my_function(self, input):
if isinstance(input, type):
...do this
elif isinstance(input, type2):
...do that
else
print("can only handle type and type2")
raise TypeError
but this is un-pythonic. How should I structure it?
In my specific use-case, I want to make a function that can handle a list
or a pandas DataFrame
, but from a function design POV, how should I design that in a try except paradigm? It kind of feels "ugly," but I also haven't seen code directly that does this in python yet.