【发布时间】:2015-05-02 12:34:11
【问题描述】:
我对 python 比较陌生,我在命名空间方面遇到了一些问题。
class a:
def abc(self):
print "haha"
def test(self):
abc()
b = a()
b.test() #throws an error of abc is not defined. cannot explain why is this so
【问题讨论】:
-
它正在工作,
class a的函数abc()被其实例调用。 -
我认为您对
b.test()的调用而不是b.abc()应该会引发错误。那是因为您应该使用类实例的引用调用abc()。只需在test()的class a函数中将abc()替换为self.abc()。