iptfile=args和args[0]或“Python/graminit.c”

2024-09-29 21:52:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我在阅读cpython/keyword.py时遇到以下代码

def main():
    import sys, re

    args = sys.argv[1:]
    iptfile = args and args[0] or "Python/graminit.c"
    if len(args) > 1: optfile = args[1]
    else: optfile = "Lib/keyword.py"

iptfile = args and args[0] or "Python/graminit.c"是什么意思?
我能理解iptfile = args or "Python/graminit.c"。你知道吗

另外,模块是在main()中导入的,而不是放在顶部,我应该遵循这个约定吗?你知道吗


Tags: orand代码pyimportremaindef
1条回答
网友
1楼 · 发布于 2024-09-29 21:52:28

这个文件几乎是生成kwlist的一种quine。它接受两个可选输入,语法和模板,并更新kwlist列表。默认情况下,它自己写!你知道吗

来表达你的具体意见

iptfile = args and args[0] or "Python/graminit.c"

这有点像穷人的表情。如果args是真的:iptfile = args[0],否则iptfile = "Python/graminit.c"也许这是一种更可读的形式

iptfile = args[0] if args else "Python/graminit.c"

不过,如果你看一下引入这段代码的the commit,它的语法甚至早于该语言(1997-03-20)中的语法!你知道吗

导入作为一种性能黑客位于函数内部。生成关键字列表文件的代码几乎从不执行,除非在构建时执行,因此在关键路径中避免了导入(这可能会很昂贵)。一般来说,这种样式是不必要的,在文件顶部导入也很好(而且通常更易于维护/协作)。你知道吗

相关问题 更多 >

    热门问题