-2

Below is the given code which gives error

def test():
    print("test")
    mylogic.runable()

class MyBackend:
    
    def runable(self):
        print("Hello")
        
def main(*args, **kwargs):
    mylogic = MyBackend()
    mylogic.runable()
    
    t1 = test()

if __name__ == "__main__":

    main()

whereas the same code with below difference works

def test():
    print("test")
    mylogic.runable()

class MyBackend:
    
    def runable(self):
        print("Hello")
        
if __name__ == "__main__":

    mylogic = MyBackend()
    mylogic.runable()
    
    t1 = test()

Difference is when object initializtion and its calling done in main than creating a main function and then add all the functions to it

Error produced for the first code is

Traceback (most recent call last):
  File "main.py", line 26, in <module>
    main()
  File "main.py", line 22, in main
    t1 = test()
  File "main.py", line 11, in test
    mylogic.runable()
NameError: name 'mylogic' is not defined

Let me know how do i resolve in using second method.

deceze
  • 491,798
  • 79
  • 706
  • 853
S F
  • 7
  • 3

0 Answers0