What should I do about object from modules not imported in the current module?
<p>I do not know if this is really a technical question but maybe more a question about good practices.</p>

<p>Suppose you write a module with several functions which work with Figure object of matplotlib. The functions get the fig object as arguments and return this fig object. For example :</p>

<pre><code>def do_smth(fig, args):
    """ do something on fig"""
    fig.suptitle("plop")
    # more stuff
    return fig
</code></pre>

<p>The above function does not need the matplotlib module to be imported.</p>

<p>I am in trouble about that. Is it ok to write a complete module with functions which work on objects coming from another module without importing this module ? Is it enough to mention this in the doc ? Is there some recommandations about this kind of cases ? And of courses have I obtained this situation because the feelings of the module is wrong ?</p>

<p>If the module is expected to work with objects from another module, I would say you should import that other module for the sake of clarity.</p>

<p>After all, Python caches imports. If the other module was already imported, attempting to import it again has no cost. If the other module was not already imported... this module, or at least the functions that work with objects from the other, is pretty much useless (because you won't be able to get objects from the other module to pass to the functions in this one).</p>

<p>The extra import exists purely for clarity - but it does provide a lot of clarity. A line in a docstring can be missed more easily than a line in the imports, if someone's checking for requirements. If you don't include the import, and someone imports your module without having the other available (having misunderstood your purpose), it'll load fine - but will probably do something they weren't expecting, if they call those functions. If you do include the import, and they try to import your module without having the other available, they will end up with an error to clue them in as to what your module is for.</p>

python matplotlib module