Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to insert return inside if __name__ == main?

This gives error:

if __name__=="__main__":
    box = Myfunc()
    box.do(1)
    if box.loop() <> Drh.DrhOk:
        return 
    else:
      ...
      ...

SyntaxError: 'return' outside function

How do I return if __name__=="__main__": block?

like image 593
alwbtc Avatar asked Feb 21 '13 11:02

alwbtc


2 Answers

You don't. Your __main__ block should always read:

if __name__ == "__main__":
    main()

Putting your code inside a main() function ensures that it is testable.

like image 155
ecatmur Avatar answered Oct 19 '22 12:10

ecatmur


You don't want to return, you probably want to stop the execution. Use sys.exit()

like image 35
Cédric Julien Avatar answered Oct 19 '22 10:10

Cédric Julien