我在阅读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()
中导入的,而不是放在顶部,我应该遵循这个约定吗?你知道吗
这个文件几乎是生成
kwlist
的一种quine。它接受两个可选输入,语法和模板,并更新kwlist
列表。默认情况下,它自己写!你知道吗来表达你的具体意见
这有点像穷人的表情。如果
args
是真的:iptfile = args[0]
,否则iptfile = "Python/graminit.c"
也许这是一种更可读的形式不过,如果你看一下引入这段代码的the commit,它的语法甚至早于该语言(1997-03-20)中的语法!你知道吗
导入作为一种性能黑客位于函数内部。生成关键字列表文件的代码几乎从不执行,除非在构建时执行,因此在关键路径中避免了导入(这可能会很昂贵)。一般来说,这种样式是不必要的,在文件顶部导入也很好(而且通常更易于维护/协作)。你知道吗
相关问题 更多 >
编程相关推荐