简体   繁体   中英

How to break from `__main__` without exit()

I'm doing python coding with Emacs.

I find it troublesome to get Emacs inferior shell exited whenever I call sys.exit . How can the code break from __main__ block without killing Emacs inferior shell process, without introducing another indented block?

if __name__ == "__main__":
    # doing something
    if args.init:
        init_env(cfg_dict, args)
        exit(0)   # <--- this kills the Emacs sub-shell
    # otherwise doing something
    # ...

PS I slept on the title of this question for a while, but I couldn't think of better title. :-(

Why not wrap the main code in a function and make use of return :

def main():
    # doing something
    if args.init:
        init_env(cfg_dict, args)
        return

if __name__ == "__main__":
    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM