I'm developing a module that should be able to interact with other modules such as numpy or PIL. To accept parameters and test if, for example, a parameter is an
numpy.ndarray, I'd need to import numpy first.
What I used to do on previous projects is
numpy_exists = True try: import numpy as np except: numpy_exists = False
Then, when accepting parameters that could be of type
<type numpy.ndarray> I checked it like that:
if numpy_exists and type(param) == np.ndarray: # ...
Now, it works, but it feels very weak because (personally) I look at the statement and the only thing I see is the
NameError this code could raise. Of course, it doesn't raise the error, but code like that makes me feel awkward when looking at it.
Is this a good way to handle the problem? Is there a better one?