【问题标题】:NameError: global name 'myExample2' is not defined # modulesNameError:未定义全局名称“myExample2”# 模块
【发布时间】:2013-11-15 09:54:16
【问题描述】:

这是我的example.py 文件:

from myimport import *
def main():
    myimport2 = myimport(10)
    myimport2.myExample() 

if __name__ == "__main__":
    main()

这里是myimport.py 文件:

class myClass:
    def __init__(self, number):
        self.number = number
    def myExample(self):
        result = myExample2(self.number) - self.number
        print(result)
    def myExample2(num):
        return num*num

当我运行example.py 文件时,出现以下错误:

NameError: global name 'myExample2' is not defined

我该如何解决这个问题?

【问题讨论】:

  • 你需要myExample2(self, num),然后将其称为self.myExample2()

标签: python class python-3.x module


【解决方案1】:

这是对您的代码的简单修复。

from myimport import myClass #import the class you needed

def main():
    myClassInstance = myClass(10) #Create an instance of that class
    myClassInstance.myExample() 

if __name__ == "__main__":
    main()

还有myimport.py

class myClass:
    def __init__(self, number):
        self.number = number
    def myExample(self):
        result = self.myExample2(self.number) - self.number
        print(result)
    def myExample2(self, num): #the instance object is always needed 
        #as the first argument in a class method
        return num*num

【讨论】:

    【解决方案2】:

    我在您的代码中看到两个错误:

    1. 您需要将myExample2 称为self.myExample2(...)
    2. 定义myExample2时需要添加selfdef myExample2(self, num): ...

    【讨论】:

      【解决方案3】:

      首先,我同意 alKid 的回答。这实际上更像是对问题的评论而不是答案,但我没有评论的声誉。

      我的评论:

      导致错误的全局名称是myImport 而不是myExample2

      解释:

      我的 Python 2.7 生成的完整错误消息是:

      Message File Name   Line    Position    
      Traceback               
          <module>    C:\xxx\example.py   7       
          main    C:\xxx\example.py   3       
      NameError: global name 'myimport' is not defined
      

      当我试图在自己的代码中追踪一个晦涩的“未定义全局名称”错误时,我发现了这个问题。因为问题中的错误信息不正确,我最终更加困惑。当我实际运行代码并看到实际错误时,一切都说得通了。

      我希望这可以防止任何找到这个帖子的人遇到与我相同的问题。如果比我更有名望的人想将其变成评论或解决问题,请随时。

      【讨论】:

      • 对于所有批评我没有回答问题的人,我深表歉意。 TLDR:问题中的代码示例不会产生错误。问题无法回答!
      【解决方案4】:

      您必须创建myClass 类的实例,而不是整个模块的实例(我编辑变量名称以使其不那么糟糕):

      from myimport import *
      def main():
          #myobj = myimport.myClass(10)
          # the next string is similar to above, you can do both ways
          myobj = myClass(10)
          myobj.myExample() 
      
      if __name__ == "__main__":
          main()
      

      【讨论】:

        【解决方案5】:

        虽然其他答案是正确的,但我想知道是否真的需要 myExample2() 作为一种方法。你也可以独立实现它:

        def myExample2(num):
            return num*num
        
        class myClass:
            def __init__(self, number):
                self.number = number
            def myExample(self):
                result = myExample2(self.number) - self.number
                print(result)
        

        或者,如果你想保持你的命名空间干净,将它作为一个方法来实现,但因为它不需要self,作为@staticmethod

        def myExample2(num):
            return num*num
        
        class myClass:
            def __init__(self, number):
                self.number = number
            def myExample(self):
                result = self.myExample2(self.number) - self.number
                print(result)
            @staticmethod
            def myExample2(num):
                return num*num
        

        【讨论】:

          猜你喜欢
          • 2011-04-27
          • 1970-01-01
          • 2014-10-24
          • 2012-05-29
          • 2013-08-23
          • 2014-04-03
          • 2017-05-12
          • 2015-09-01
          相关资源
          最近更新 更多