将 python 模块导入例程或类定义有什么问题吗?
可能的重复:
Python 导入语句是否应该始终位于模块顶部?
我最近回答了所以问题并提供此例程作为解决方案:
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)
我将 matplotlib
导入到 set_fontsize(fig,fontsize)
的定义中,因为它是不保证使用此例程的人会在更全局的范围内导入 matplotlib (更好的术语?)。特别是因为许多 matplotlib 示例使用此导入调用例程:import matplotlib.pyplot as plt
。
是否存在导入 matplotlib 会导致冲突的情况?
有效率成本吗?
是否有更好/更常见的替代方法来测试 fig
是否是 matplotlib.figure.Figure
的实例;不需要导入模块的替代方案?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
导入内部函数和类没有任何问题 - 例如,这是处理相互递归导入(其中两个文件各自导入另一个文件)的有用方法。
然而,检查参数类型有问题。惯用的Python不会检查fig的类型。相反,只要滥用就失败。这是因为你打破了“鸭子打字”——人们不能用“像”无花果一样工作的对象来调用你的例程,即使他们想这样做(一个明显的例子是测试模拟;另一个例子是有人编写了 matplotlib 的替代品,它具有相同的 API,但看起来或工作得更好)。
因此,对于您那里的代码,根本没有必要进行导入。只需使用
fig
。更一般地说,导入在第一次使用时会被缓存,因此您通常不需要太担心效率(我并不是说它是完美的,但这是您在担心之前需要分析的事情)。
there's nothing wrong with importing inside functions and classes - it's a useful way of handling mutually recursive imports (where two files each imports the other), for example.
however, there is something wrong with checking the type of an argument. idiomatic python would not check the type of fig. instead, just let misuse fail wherever it fails. this is because you are breaking "duck typing" - people cannot call your routine with objects that "work like" fig, even if they want to (an obvious example is testing mocks; another example is someone writing a replacement for matplotlib that has the same API, but looks or works better).
so, for the code you have there, it is not necessary to have the import at all. just use
fig
.more generally, imports are cached when first used, so you typically don't need to worry much about efficiency (i'm not saying it's perfect, but it's the kind of thing you need to profile before worrying about).
在函数内部导入并没有什么特别的错误 - 尽管你应该在文档字符串之后进行导入,否则 Python 将看不到文档字符串 - 但你的推理没有任何意义。
如果您在模块级别导入,并且有人导入您的函数,则该函数可以访问其模块中的所有内容,包括导入。您的函数的用户不需要专门导入任何内容。
There's nothing particularly wrong with importing inside the function - although you should do it after the docstring, otherwise Python won't see the docstring - but your reasoning doesn't make any sense.
If you import at module level, and someone imports your function, the function has access to all the things in its module, including imports. Users of your function don't need to import anything specifically.