[原文]P110
Python 2.2 introduced a subtle but important change that affects the namespace search order: nested scopes. In versions of Python prior to 2.2, when you reference a variable within a nested function or lambda function, Python will search for that variable in the current (nested or lambda) function's namespace, then in the module's namespace. Python 2.2 will search for the variable in the current (nested or lambda) function's namespace, then in the parent function's namespace, then in the module's namespace. Python 2.1 can work either way; by default, it works like Python 2.0, but you can add the following line of code at the top of your module to make your module work like Python 2.2:
from __future__ import nested_scopes
Are you confused yet? Don't despair! This is really cool, I promise. Like many things in Python, namespaces are directly accessible at run−time. How? Well, the local namespace is accessible via the built−in locals function, and the global (module level) namespace is accessible via the built−in globals function.
[pope改进译]
Python 2.2包含一个不容易引起注意但是很重要的改变,对命名空间的搜索顺序的影响:嵌套作用域{nested scopes}。在Python2.2之前{prior to}的版本,当你引用一个嵌入函数或lambda函数{[?]lambda function 是什么函数?}的变量。Python将先在当前(嵌入或lambda)函数的命名空间中查询,之后是这个模块的命名空间。Python2.2 中将先在当前(嵌入或lambda)函数的命名空间中查询,之后在此函数调用者的命名空间,之后是当前模块的命名空间。Python2.1可以工作在任何一种工作方式中,默认情况,它将向Python2.0一样,但是你可以增加如下的代码在你当前模块的头,使它像Python2.2一样工作:
from __future__ import nested_scopes
是不是感觉有点困惑?别担心,我到认为,这个真的很棒。像Python中的很多事情一样,命名空间是在运行时动态加载的。如何完成呢?很好,本地命名空间通过嵌入的locals函数读取,全局命名空间(模块级别)通过嵌入的globals函数读取。
---------------------------------------------------------
[pope译]
Python 2.2包含一个不容易引起注意但是很重要的改变,对命名空间的搜索顺序的影响:嵌套作用域{nested scopes}。在Python2.2之前{prior to}的版本,当你引用一个嵌入函数或lambda函数{[?]lambda function 是什么函数?}的变量。Python将先在当前(嵌入或lambda)函数的命名空间中查询,之后是这个模块的命名空间。Python2.2 中将先在当前(嵌入或lambda)函数的命名空间中查询,之后在当前函数的命名空间,之后是当前模块的命名空间。Python2.1可以工作在任何一种工作方式中,默认情况,它将向Python2.0一样,但是你可以增加如下的代码在你当前模块的头,使它像Python2.2一样工作:
from __future__ import nested_scopes
是不是感觉有点困惑?别担心,我到认为,这个真的很棒。像Python中的很多事情一样,命名空间是直接在运行时刻存取的。如何完成呢?很好,本地命名空间通过嵌入的本地函数读取,全局命名空间(模块级别)通过嵌入的全局函数读取。
[net 译 来源:http://woodpecker.org.cn/diveintopython/html_processing/locals_and_globals.html]
Python 2.2 引入了一种略有不同但重要的改变,它会影响名字空间的搜索顺序:嵌套的作用域。 在 Python 2.2
版本之前,当您在一个嵌套函数或 lambda 函数中引用一个变量时,Python 会在当前
(嵌套的或 lambda)
函数的名字空间中搜索,然后在模块的名字空间。Python 2.2
将只在当前 (嵌套的或 lambda)
函数的名字空间中搜索,然后是在父函数的名字空间 中搜索,接着是模块的名字空间中搜索。Python 2.1
可 以两种方式工作,缺省地,按 Python 2.0
的方式工作。但是您可以把下面一行代码增加到您的模块头部,使您的模块工作起来像 Python 2.2
的方式:
from __future__ import nested_scopes |
您是否为此而感到困惑?不要灰心!我敢说这一点非常酷。像 Python 中的许多事情一样,名字空间在运行时直接可以访问。怎么样?不错吧,局部名字空间可以通过内置的 locals 函数来访问。全局
(模块级别) 名字空间可以通过内置的 globals 函数来访问。
[popexizhi:]
lambda 函数: 从lisp借用来的最小的函数的定义eg:
g = lambda x: x*2 then in the parent function's namespace, [pope译]之后在当前函数的命名空间, [net 译]然后是在父函数的名字空间 中搜索, [popexizhi] the parent function pope将的翻译给丢了,插一句:这种问题是好事,错误时正确的不错的开始,不做才是问题,这个idea自己要好好吸取一下,之前自己对错误的恐惧是有问题:) namespaces are directly accessible at run−time. [pope译]命名空间是直接在运行时刻存取的 [net 译]名字空间在运行时直接可以访问 [popexizhi] 这里的directly accessible 应该说的是动态加载的表达吧?net译在这节中的感觉不是专业codeing的翻译,namespcaes 就是个不错的例子。好了不管它了,翻译自己的。
How? Well, the local namespace is accessible via the built−in locals function, and the global (module level) namespace is accessible via the built−in globals function.[pope译]如何完成呢?很好,本地命名空间通过嵌入的本地函数读取,全局命名空间(模块级别)通过嵌入的全局函数读取。 [net 译]怎么样?不错吧,局部名字空间可以通过内置的 locals 函数来访问。全局 (模块级别) 名字空间可以通过内置的 globals 函数来访问。 [popexizhi] the built-in locals function和 the built-in globals function 翻译net做的对,不翻译才应该是内嵌的locals函数和globals函数
没有评论:
发表评论