简体   繁体   English

if __name__ == '__main__' 的快捷方式:

[英]Shortcut for if __name__ == '__main__':

Is there a shorter form of this?有更短的形式吗?

if __name__ == '__main__':

It is pretty tedious to write, and also doesn't look very nice in my opinion :)写起来很乏味,在我看来也不太好看:)

PEP299 proposed a solution to this wart, namely having a special function name __main__ . PEP299 针对这个问题提出了一个解决方案,即具有一个特殊的函数名称__main__ It was rejected, partly because:它被拒绝,部分原因是:

Guido pronounced that he doesn't like the idea anyway as it's "not worth the change (in docs, user habits, etc.) and there's nothing particularly broken." Guido 表示他无论如何都不喜欢这个想法,因为它“不值得改变(在文档、用户习惯等方面),而且没有什么特别坏的地方。”

http://www.python.org/dev/peps/pep-0299/ http://www.python.org/dev/peps/pep-0299/

So the ugliness will stay, at least as long as Guido's the BDFL.所以丑陋会留下来,至少只要圭多是 BDFL。

Basically every python programmer does that.基本上每个 Python 程序员都会这样做。 So simply live with it.所以简单地忍受它。 ;) ;)

Besides that you could omit it completely if your script is always meant to be run as an application and not imported as a module - but you are encouraged to use it anyway, even if it's not really necessary.除此之外,如果您的脚本始终打算作为应用程序运行而不是作为模块导入,您可以完全省略它 - 但我们鼓励您无论如何都使用它,即使它不是真正必要的。

After asking this question, I decided to make a solution to it:在问了这个问题之后,我决定解决它:

from automain import *  # will only import the automain decorator

@automain
def mymain():
    print 'this is our main function'

The blog post explains it, and the code is on github and can be easy_installed:博文解释了,代码在github上,可以easy_installed:

easy_install automain

It's definitely a wart in the language, as is anything that becomes boilerplate and gets copied and pasted from file to file.这绝对是语言中的一个问题,就像任何成为样板并从文件复制和粘贴到文件的东西一样。 There's no shorthand for it.它没有简写。

As warts and boilerplate go, though, at least it's minor.然而,随着疣和样板的发展,至少它是次要的。

你的意思是更短,比如if'__main__'==__name__:

更短,如果你计算行:

__name__ == '__main__' and main()

No, sorry, there is not.不,抱歉,没有。 It doesn't look great, but it's what we've got.它看起来不太好,但这就是我们所拥有的。

It is pretty tedious to write, and also doesn't look very nice in my opinion :)写起来很乏味,在我看来也不太好看:)

My perfectionism also finds Python main sa tad ugly.我的完美主义还发现Python main有点丑。 So, I searched for solutions and finally used the following code.所以,我搜索了解决方案,最后使用了以下代码。

Copy / pasted code :复制/粘贴代码:

# main_utils.py
import inspect
from types import FrameType
from typing import cast


def is_caller_main() -> bool:
    # See https://stackoverflow.com/a/57712700/
    caller_frame = cast(FrameType, cast(FrameType, inspect.currentframe()).f_back)
    caller_script_name = caller_frame.f_locals['__name__']

    return caller_script_name == '__main__'
#!/usr/bin/env python3

# test.py
# Use case

import main_utils


if main_utils.is_caller_main():
    print('MAIN')
else:
    print('NOT MAIN')

Source on GitHub Gist : GitHub Gist上的来源:

 <script src="https://gist.github.com/benoit-dubreuil/fd3769be002280f3a22315d58d9976a4.js"></script>

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

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