【发布时间】:2014-06-28 12:59:53
【问题描述】:
任何线索为什么我的类中的方法编译并说在我尝试运行它时没有声明它?任何人都可以在代码中看到,function2 是在类中声明的:
class MyClass():
def __init__(self):
pass
def function2(self,myfilename):
file = open(myfilename, "r")
for line in file:
print(line, end='')
file.close()
def function1(self,myfilename):
function2(myfilename)
def main():
myfilename = "input.txt"
obj = MyClass()
obj.function1(myfilename)
if __name__ == '__main__':
main()
我编译代码没有问题。但是在尝试运行时,它会说:
NameError: name 'function2' is not defined
为什么编译得很好,但是运行时却崩溃了?有什么建议吗?
【问题讨论】:
-
什么编译器?你是如何编译python的?
-
@mushroom - 使用 Geany 在 python3 上运行:python3 -m py_compile "test.py"
-
你使用py_compile的原因是什么?请注意,您不必在运行 python 代码之前对其进行编译。
标签: python debugging python-3.x compiler-construction error-handling