简体   繁体   English

Python 中的顶级语句是什么?

[英]What is a top-level statement in Python?

In the Python Guide's chapter on project structure , the term "top-level statement" is brought up a few times.在 Python 指南关于 项目结构的章节中,术语“顶级语句”被提及了几次。 I'm not sure exactly what this refers to.我不确定这到底指的是什么。 My guess is it's any variable declarations that happen outside of any functions or class methods that fire as soon as a module is loaded.我的猜测是它是在加载模块后立即触发的任何函数或类方法之外发生的任何变量声明。 Is this correct?这样对吗? Does it also include a module's import statements?它还包括模块的import语句吗?

It's not just variable declarations (and there aren't any variable declarations anyway).这不仅仅是变量声明(无论如何也没有任何变量声明)。 It's pretty much anything that starts at indentation level 0.几乎所有从缩进级别 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

Here's the first mention of "top-level statement":这是第一次提到“顶级声明”:

Once modu.py is found, the Python interpreter will execute the module in an isolated scope.一旦找到 modu.py,Python 解释器将在隔离的范围内执行该模块。 Any top-level statement in modu.py will be executed, including other imports if any. modu.py 中的任何顶级语句都将被执行,包括其他导入(如果有)。 Function and class definitions are stored in the module's dictionary.函数和类定义存储在模块的字典中。

This makes it clear that what they really mean is "things that are interpreted at import time".这清楚地表明它们的真正含义是“在import时解释的事物”。

While it's not terribly helpful directly, the Python documentation itself also uses the phrase "top-level" (components, which then means "statements" in this context).虽然直接没有太大帮助,但Python 文档本身也使用了“顶级”一词(组件,在此上下文中表示“语句”)。

Note that this module:注意这个模块:

"""a python module, spam.py"""

def spam():
    return "spam"

class Spam(object):
    pass

has two statements in it, the def and the class .其中有两个语句, defclass These are both executed at import time.这些都在导入时执行 These definitions are compound statements (see def and class descriptions).这些定义是复合语句(参见defclass描述)。 If there are decorators attached to a top-level def , that adds even more top-level things to run.如果有装饰器附加到顶级def ,则会添加更多要运行的顶级内容。 (See also user2357112's answer : running a class statement invokes more internal workings.) (另请参阅user2357112 的回答:运行class语句会调用更多内部工作。)

Add an import sys at the top and you've added a third statement, which imports sys .在顶部添加一个import sys并且您已经添加了第三个语句,它导入sys However, if you add this:但是,如果您添加以下内容:

def ham(eggs):
    import os
    return os.path.basename(eggs)

you have still only added one statement, the def ham , to the top-level stuff.您仍然只在顶级内容中添加了一个语句def ham It's when ham itself is executed (called) that the import os will be run.ham本身被执行(调用)时, import os将被运行。

我认为你完全正确,是的,那将包括import语句。

In python, the statements which are not indented are called a top-level statement.在python中,没有缩进的语句称为顶级语句。 Internally python gives a special name to top-level statement s as __main__ .在内部,python 为顶级语句 s 提供了一个特殊名称__main__

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

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