Often in my own personal Python libraries, I do something like this:
class MyClass: # ... def plot(self): import someGraphicsLibrary as graphicslib graphicslib.plot(self.data)
The reason is that initialising
someGraphicsLibrary takes some time, up to a few seconds for one of the libraries I use. I don't always need to plot my results when I use this class, so it makes sense not to import it until the time it's actually used, if at all.
This seems to work fine, but I don't think I've seen it in anyone else's code. So my question is simply whether this is considered a good practice. Are there any hidden pitfalls to be expected when doing things this way?