频道栏目
首页 > 程序开发 > Web开发 > python教程 > Python v3.4.3 语言参考 > 正文
4. 执行模型 Python v3.4.3 语言参考
2016-01-11 18:01:12      个评论      
收藏   我要投稿

4.1. 命名与绑定

变量名 指定对象. 变量通过名称绑定完成. 每一个变量通过绑定 之后供程序调用.

一个是一段单独python代码.块包括: 一个模块,一段方法体,一个类. 每个可交互的命令的是一个块. 一个脚本文件 (可以被解释器当做标准输出或命令行参数)是一段代码块. 一个脚本命令(一个指定了 ‘-c‘ 选项的命令行)是一段代码块. 需要传入字符参数的内建方法 eval() 和 exec() 也是代码块.

一个代码块就是一个 执行片段一个片段包含一些信息 (用于调试) 且决定在何时并以何种方式继续执行下一段代码.

 scope 定义了在一个块内变量的可见性. 如果在一个块内定义的变量,他的scope就是这个块.如果定义在一个方法块中, scope为所有调用此方法的块,除了那些给这个变量重新绑定成其它对象的块之外. 变量在一个类块中的scope为当前的类; 但这并非说明可以在类中任意使用. 也就是说下面这段代码执行会失败:

class A: a = 42 b = list(a + i for i in range(10)) 

当一个代码块中使用一个变量名时 它只能用于最近的scope. 这些对于代码块的可见范围被称做代码块的 环境.

如果一个变量名被绑定到一个块内,那么它对于这个块来说就是一个局部变量除非将其声明为nonlocal如果一个变量名被绑定在一个模块的范围内,那么它是一个全局变量. (定义在模块中的变量既是本地变量也是全局变量.) 如果一个变量在代码块中只使用但未定义,那么他是一个 自由变量.

在“变量名“根本找不到时,就会抛出 NameError 异常. 如果变量名引用了一个未绑定的局部变量,就会抛出 UnboundLocalError 异常。 UnboundLocalError 是 NameError的一个子类。

The following constructs bind names: formal parameters to functions, import statements, class and function definitions (these bind the class or function name in the defining block), and targets that are identifiers if occurring in an assignment, for loop header, or after as in a with statement or except clause. The import statement of the form from ... import * binds all names defined in the imported module, except those beginning with an underscore. This form may only be used at the module level.

A target occurring in a del statement is also considered bound for this purpose (though the actual semantics are to unbind the name).

Each assignment or import statement occurs within a block defined by a class or function definition or at the module level (the top-level code block).

If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound.This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations.

If the global statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module builtinsThe global namespace is searched first. If the name is not found there, the builtins namespace is searched. The global statement must precede all uses of the name.

The builtins namespace associated with the execution of a code block is actually found by looking up the name __builtins__ in its global namespace; this should be a dictionary or a module (in the latter case the module’s dictionary is used). By default, when in the __main__ module, __builtins__ is the built-in module builtinswhen in any other module, __builtins__ is an alias for the dictionary of the builtins module itself. __builtins__ can be set to a user-created dictionary to create a weak form of restricted execution.

CPython implementation detail: Users should not touch __builtins__it is strictly an implementation detail. Users wanting to override values in the builtins namespace should import the builtins module and modify its attributes appropriately.

The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called __main__.

The global statement has the same scope as a name binding operation in the same block. If the nearest enclosing scope for a free variable contains a global statement, the free variable is treated as a global.

A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution. The namespace of the class definition becomes the attribute dictionary of the class.Names defined at the class scope are not visible in methods.

4.1.1. Interaction with dynamic features

There are several cases where Python statements are illegal when used in conjunction with nested scopes that contain free variables.

If a variable is referenced in an enclosing scope, it is illegal to delete the name. An error will be reported at compile time.

The eval() and exec() functions do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The exec() and eval() functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both.

4.2. Exceptions

异常是指代码片段中,可以捕获错误或者其他意外情况的流程中断An exception is raised at the point where the error is detected; it may be handled by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred.

The Python interpreter raises an exception when it detects a run-time error (such as pision by zero). A Python program can also explicitly raise an exception with the raise statement. Exception handlers are specified with the try ...except statement. The finally clause of such a statement can be used to specify cleanup code which does not handle the exception, but is executed whether an exception occurred or not in the preceding code.

Python uses the “termination” model of error handling: an exception handler can find out what happened and continue execution at an outer level, but it cannot repair the cause of the error and retry the failing operation (except by re-entering the offending piece of code from the top).

When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop. In either case, it prints a stack backtrace, except when the exception is SystemExit.

Exceptions are identified by class instances. The except clause is selected depending on the class of the instance: it must reference the class of the instance or a base class thereof. The instance can be received by the handler and can carry additional information about the exceptional condition.

Note

 

Exception messages are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter.

See also the description of the try statement in section The try statement and raise statement in section The raise statement.

Footnotes

[1] This limitation occurs because the code that is executed by these operations is not available at the time the module is compiled.

点击复制链接 与好友分享!回本站首页
相关TAG标签 模型 语言
上一篇:3. 数据类型 Python v3.4.3 语言参考
下一篇:5. 导入系统 Python v3.4.3 语言参考
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站