Python NameError: 全局名称 'myExample2' 未定义 # modules

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19998190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:12:29  来源:igfitidea点击:

NameError: global name 'myExample2' is not defined # modules

pythonclasspython-3.xmodule

提问by Michael

Here is my example.pyfile:

这是我的example.py文件:

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

if __name__ == "__main__":
    main()

And here is myimport.pyfile:

这是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

When I run example.pyfile, i have the following error:

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

NameError: global name 'myExample2' is not defined

How can I fix that?

我该如何解决?

采纳答案by aIKid

Here's a simple fix to your code.

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

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()

And the myimport.py:

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

回答by shx2

I see two errors in you code:

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

  1. You need to call myExample2as self.myExample2(...)
  2. You need to add selfwhen defining myExample2: def myExample2(self, num): ...
  1. 你需要调用myExample2self.myExample2(...)
  2. self定义myExample2时需要添加:def myExample2(self, num): ...

回答by Feanor

You have to create an instance of the myClassclass, and not the instance of the whole module(and i edit variables names to be less awful):

您必须创建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()

回答by glglgl

While the other answers are correct, I wonder if there is really a need for myExample2()being a method. You could as well implement it standalone:

虽然其他答案是正确的,但我想知道是否真的需要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)

Or, if you want to keep your namespace clean, implement it as a method, but as it doesn't need self, as a @staticmethod:

或者,如果您想保持命名空间干净,请将其实现为一个方法,但因为它不需要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

回答by Dennis

First, I agree in with alKid's answer. This is really more a comment on the question than an answer, but I don't have the reputation to comment.

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

My comment:

我的评论:

The global name that causes the error is myImportnot myExample2

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

Explanation:

解释:

The full error message generated by my Python 2.7 is:

我的 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

I found this question when I was trying to track down an obscure "global name not defined" error in my own code. Because the error message in the question is incorrect, I ended up more confused. When I actually ran the code and saw the actual error, it all made sense.

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

I hope this prevents anyone finding this thread from having the same problem I did. If someone with more reputation than I wants to turn this into a comment or fix the question, please feel free.

我希望这可以防止任何发现此线程的人遇到与我相同的问题。如果有人比我更有声望,想把它变成评论或解决问题,请随意。