简体   繁体   English

如何在没有exit()的情况下脱离__main__

[英]How to break from `__main__` without exit()

I'm doing python coding with Emacs. 我正在用Emacs做python编码。

I find it troublesome to get Emacs inferior shell exited whenever I call sys.exit . 我发现每次调用sys.exit退出Emacs下层外壳很麻烦。 How can the code break from __main__ block without killing Emacs inferior shell process, without introducing another indented block? 代码如何从__main__块中断开而又不杀死Emacs劣质的shell进程,又不引入另一个缩进块?

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. PS我睡了一段时间这个问题的标题,但我想不出更好的标题。 :-( :-(

Why not wrap the main code in a function and make use of return : 为什么不将主代码包装在函数中并利用return

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

if __name__ == "__main__":
    main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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