Actually forgetting would be a better way to describe what actually happens Of

Actually forgetting would be a better way to describe

This preview shows page 95 - 97 out of 135 pages.

function returns or raises an exception that is not handled within the function. (Actually, forgetting would be a better way to describe what actually happens.) Of course, recursive invocations each have their own local namespace. 当函数被调用时创建一个局部命名空间,函数反正返回过抛出一个未在函数内处 理的异常时删除。(实际 上,说是遗忘更为贴切)。当然,每一个递归调用拥有 自己的命名空间。 A scope is a textual region of a Python program where a namespace is directly accessible. ``Directly accessible'' here means that an unqualified reference to a name attempts to find the name in the namespace. 作用域 是Python程序中一个命名空间可以直接访问的文法区域。“直接访问” 在这里的意思是查找命名时 无需引用命名前缀。 Although scopes are determined statically, they are used dynamically. At any time during execution, there are at least three nested scopes whose namespaces are directly accessible: 尽管作用域是静态定义,在使用时他们都是动态的。每次执行时,至少有三个命 名空间可以直接访问的作 用域嵌套在一起:包含局部命名的使用域在最里面,首 先被搜索;其次搜索的是中层的作用域,这里包含 了同级的函数;最后搜索最外 面的作用域,它包含内置命名。 the innermost scope, which is searched first, contains the local names 首先搜索最内层的作用域,它包含局部命名 the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contains non-local, but also non-global names 任意函数包含的作用域,是内层嵌套作用域搜索起点,包含非局部,但是也非 全局的命名 2 有一个例外。模块对象有一个隐秘的只读对象,名为 dict ,它 返回用于实现模块命名空间的字典,命名 dict 是一个 属性而非 全局命名。显然,使用它违反了命名空间实现的抽象原则,应该被严格限制于 调试中。 9.2. Python Scopes and Namespaces Python 作用域和命名空间 89
Image of page 95
Python Tutorial, Release 2.7 the next-to-last scope contains the current module's global names 接下来的作用域包含当前模块的全局命名 the outermost scope (searched last) is the namespace containing built-in names 最外层的作用域(最后搜索)是包含内置命名的命名空间。 If a name is declared global, then all references and assignments go directly to the middle scope containing the module's global names. Otherwise, all variables found outside of the innermost scope are read-only (an attempt to write to such a variable will simply create a new local variable in the innermost scope, leaving the identically named outer variable unchanged). 如果一个命名声明为全局的,那么所有的赋值和引用都直接针对包含模全局命名 的中级作用域。另外,从 外部访问到的所有内层作用域的变量都是只读的。(试 图写这样的变量只会在内部作用域创建一个 新 局 部变量,外部标示命名的那 个变量不会改变)。 Usually, the local scope references the local names of the (textually) current function. Outside functions, the local scope references the same namespace as the global scope: the module's namespace. Class definitions place yet another namespace in the local scope. 通常,局部作用域引用当前函数的命名。在函数之外,局部作用域与 全局使用域引用同一命名空间:模块 命名空间。类定义也是局部作用域中的另一 个命名空间。 It is important to realize that scopes are determined textually: the global scope of a function defined in a module is that module's namespace, no matter from where or by what alias the function is called. On the other hand, the actual search for names is done dynamically, at run time --- however, the language definition is evolving towards static name resolution, at ``compile'' time, so don't rely on dynamic name resolution! (In fact, local variables are already determined statically.) 重要的是作用域决定于源程序的意义:一个定义于某模块中的函数的全局作用域 是该模块的命名空间,而 不是该函数的别名被定义或调用的位置,了解这一点非 常重要。另一方面,命名的实际搜索过程是动态 的,在运行时确定的——然 而,Python 语言也在不断发展,以后有可能会成为静态的“编译”时确定,所 以不要依赖动态解析!(事实上,局部变量已经是静态确定了。) A special quirk of Python is that -- if no global statement is in effect -- assignments to names always go into the innermost scope.
Image of page 96
Image of page 97

You've reached the end of your free preview.

Want to read all 135 pages?

  • Fall '16
  • Shell

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture