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

后端 未结 5 1538
栀梦
栀梦 2021-01-12 03:48

Here is my example.py file:

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

if __name__ == \"__main_         


        
5条回答
  •  逝去的感伤
    2021-01-12 04:11

    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
    

提交回复
热议问题