Python 方法未定义-编译器如何遗漏它?

Python 方法未定义-编译器如何遗漏它?,python,debugging,python-3.x,compiler-construction,error-handling,Python,Debugging,Python 3.x,Compiler Construction,Error Handling,为什么我的类中的方法编译并说在我尝试运行它之后没有声明它,有什么线索吗?正如任何人在代码中看到的,function2在类中声明: class MyClass(): def __init__(self): pass def function2(self,myfilename): file = open(myfilename, "r") for line in file: print(line, end='') fil

为什么我的类中的方法编译并说在我尝试运行它之后没有声明它,有什么线索吗?正如任何人在代码中看到的,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

为什么编译得很好,但在运行时会崩溃?有什么建议吗?

替换以下内容-

  def function1(self,myfilename):
      function2(myfilename)
与-

  def function1(self,myfilename):
      self.function2(myfilename)

什么编译器?你是如何编译python的?@蘑菇-在python3上运行,Geany:python3-m py_compile“test.py”你使用py_compile的原因是什么?正如您所知,在运行python代码之前不必编译它。我正在尝试。但是,据说我至少要等4分钟才能接受。