Possible Duplicate:
Should Python import statements always be at the top of a module?
I recently answered a SO question and provided this routine as a solution:
def set_fontsize(fig,fontsize):
import matplotlib
"""
For each text object of a figure fig, set the font size to fontsize
"""
if not isinstance(fig,matplotlib.figure.Figure):
raise Exception("fig is not a matplotlib.figure.Figure")
for textobj in fig.findobj(match=matplotlib.text.Text):
textobj.set_fontsize(fontsize)
I imported matplotlib
into the definition of set_fontsize(fig,fontsize)
because it's not guaranteed that someone using this routine would import matplotlib at a more-global scope (better terminology?). Especially since many of the matplotlib examples invoke routines using this import: import matplotlib.pyplot as plt
.
Are there instances where my import of matplotlib would cause a conflict?
Are there any efficiency costs?
Is there a preferable/more-common alternative to test if fig
is an instance of matplotlib.figure.Figure
; an alternative that does not require importing the module?