NameError: The global name 'myExample2' is not defined. # Modules

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

 NameError: global name 'myExample2' is not defined 

How can i fix this?

+6
source share
5 answers

Here is a simple fix for 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 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 
+6
source

I see two errors in your code:

  • You need to call myExample2 as self.myExample2(...)
  • You need to add self when defining myExample2: def myExample2(self, num): ...
+7
source

You need to create an instance of the myClass class, not an instance of the entire module (and change the variable names less horribly):

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

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

 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 your namespace to be clean, implement it as a method, but since it does not need self , like @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 
0
source

Firstly, I agree with alKid's answer. This is really more of a comment on a question than an answer, but I have no reputation for comment.

My comment:

The global name causing the error, myImport not myExample2

Explanation:

The full error message generated by my 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 the obscure "global name undefined" error in my own code. Since the error message in the question is incorrect, I was more confused. When I actually ran the code and saw the actual error, it all made sense.

Hopefully this will not allow anyone to find this thread from the same problem as me. If someone with a greater reputation than I want to turn this into a comment or fix a question, please feel free to.

0
source

Source: https://habr.com/ru/post/958135/


All Articles