在 Python 指南的章节中项目结构 http://docs.python-guide.org/en/latest/writing/structure.html中,“高层声明”一词被多次提及。我不确定这到底指的是什么。我的猜测是,在加载模块后立即触发的任何函数或类方法之外发生的任何变量声明。它是否正确?它还包括模块的import
声明?
它不仅仅是变量声明(而且根本没有任何变量声明)。它几乎是从缩进级别 0 开始的任何内容。
import sys # top-level
3 + 4 # top-level
x = 0 # top-level
def f(): # top-level
import os # not top-level!
return 3 # not top-level
if x: # top-level
print 3 # not top-level
else:
print 4 # not top-level, but executes as part of an if statement
# that is top-level
class TopLevel(object): # top-level
x = 3 # not top-level, but executes as part of the class statement
def foo(self): # not top-level, but executes as part of the class statement
print 5 # not top-level
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)