NameError: global name 'myExample2' is not

2020-03-13 06:45发布

问题:

Here is my example.py file:

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

if __name__ == "__main__":
    main()

And here is myimport.py file:

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.py file, i have the following error:

NameError: global name 'myExample2' is not defined

How can I fix that?

回答1:

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:

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:

I see two errors in you code:

  1. You need to call myExample2 as self.myExample2(...)
  2. You need to add self when defining myExample2: def myExample2(self, num): ...


回答3:

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.

My comment:

The global name that causes the error is myImport not myExample2

Explanation:

The full error message generated by my Python 2.7 is:

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.



回答4:

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

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:

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:

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:

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