python关于namespace和scope(命名空间和作用域)

1.namespace

(1)定义

A namespace is a mapping from names to objects. Most namespaces are currently implemented as Python dictionaries, but that’s normally not noticeable in any way (except for performance), and it may change in the future. Examples of namespaces are: the set of built-in names (containing functions such as abs(), and built-in exception names); the global names in a module; and the local names in a function invocation. In a sense the set of attributes of an object also form a namespace. The important thing to know about namespaces is that there is absolutely no relation between names in different namespaces; for instance, two different modules may both define a function maximize without confusion — users of the modules must prefix it with the module name.

定义: 命名空间是从名字到对象的映射(1个命名空间可能存在多对映射), 在python中通常通过字典来实现. 例如:内置名字的集合, 模块中的全局名称, 函数中的局部名称, 包括对象的属性集合也会形成命名空间.
需注意: 不同命名空间内的名字相互独立,互不影响,例如:不同模块内部可能有相同名称的函数.

(2)命名空间的创建与删除

Namespaces are created at different moments and have different lifetimes. The namespace containing the built-in names is created when the Python interpreter starts up, and is never deleted. The global namespace for a module is created when the module definition is read in; normally, module namespaces also last until the interpreter quits. The statements executed by the top-level invocation of the interpreter, either read from a script file or interactively, are considered part of a module called __ main__, so they have their own global namespace. (The built-in names actually also live in a module; this is called builtins.)
++++++++++++++++++++++++++++++++++++++++++++++++++
The local namespace for a function is created when the function is called, and deleted when the 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.

包含内置名称的命名空间在python解释器运行时创建, 永远不删除; 模块的全局名称在模块读入时创建, 直到解释器退出. 语句通过解释器的顶层调用来执行, 无论是文本式还是交互式,都被认为是__ main__模块的一部分, 因此模块具有全局空间.
函数的局部命名空间在函数被调用时生成,直到函数返回值或产生不在函数内处理的异常,命名空间被删除.递归调用的每一层具有自己的命名空间

2. scope

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.

(1)定义:作用域是python程序的一块文本区域,在这个区域内可以直接访问命名空间(对名称的引用,会在命名空间内查找)

(2)作用域内名称的查找规则:

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
•the next-to-last scope contains the current module’s global names
•the outermost scope (searched last) is the namespace containing built-in names

1.最内层作用域(运行的当前作用域),包括局部名称
2.最近一层封闭函数的作用域,包括nonlocal名称,非全局名称等
3.倒数第二层作用域,包括全局名称
4.最外层作用域,包括python内置名称

(3)其他规则

1)If a name is declared global, then all references and assignments go directly to the middle scope containing the module’s global names. To rebind variables found outside of the innermost scope, the nonlocal statement can be used; if not declared nonlocal, those variables 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).
声明global和nonlocal的情况

2)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.)
作用域是按程序文本排布的形式确定的,函数不管在哪里被调用,他的作用域永远在他被定义的地方.

3)A special quirk of Python is that – if no global statement is in effect – assignments to names always go into the innermost scope. Assignments do not copy data — they just bind names to objects. The same is true for deletions: the statement del x removes the binding of x from the namespace referenced by the local scope. In fact, all operations that introduce new names use the local scope: in particular, import statements and function definitions bind the module or function name in the local scope.
赋值不会复制数据,只会将名称和对象绑定,删除也一样:从命名空间删除名称和对象的绑定
特别的,import语句和函数定义将模块或函数名称绑定到了本地作用域.

4)The global statement can be used to indicate that particular variables live in the global scope and should be rebound there; the nonlocal statement indicates that particular variables live in an enclosing scope and should be rebound there.
global语句可用来表明某个变量属于全局作用域, 并且被重新绑定到全局作用域; nonlocal表名某个变量属于一个封闭作用域并且被重新绑定到那里.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值