【发布时间】:2016-02-20 20:24:19
【问题描述】:
什么是导入整个类方法中使用的库的 python 和有效方法。
模块级导入为:
from numpy import exp
class A:
def calc1(self): return exp(1)
def calc2(self): return exp(1)
方法级导入更干净,但我不确定每次调用方法时是否缓存或导入库:
class B:
def calc1(self):
from numpy import exp
return exp(1)
def calc2(self):
from numpy import exp
return exp(1)
最后,是否有一些类级别的导入如下?
class C:
from numpy import exp
def calc1(self): return exp(1)
def calc2(self): return exp(1)
C().calc1() # NameError: name 'exp' is not defined
【问题讨论】:
-
模块级导入通常比方法级导入更清晰 - 您可以轻松查看该文件中导入的所有内容。
标签: python class python-3.x methods import