Python Scopes and Namespaces
Posted katachi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python Scopes and Namespaces相关的知识,希望对你有一定的参考价值。
Python的作用域和命名空间
无论是OO还是过程编程,弄清楚python的作用域和命名空间都尤为重要!
Namespaces
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.
命名空间是命名到对象的映射.目前大多数命名空间是用Python的字典(dict)实现的,但是具体如何实现无关紧要,除非出于性能的考虑,并且将来可能改变其实现方式.例如命名空间是:built-in(builtins模块)的命名的集合,包括例如abs()函数或者built-in异常命名;函数调用的本地命名.在某种意义上对象的属性集也是一个命名空间.关于命名空间需要明白一件很重要的事就是不同命名空间间绝对没有任何关系;例如,两个模块可能都定义了一个函数maximize而不会混淆--用户必须以模块名为前缀来引用它们.
By the way, I use the word attribute for any name following a dot — for example, in the expression z.real, real is an attribute of the object z. Strictly speaking, references to names in modules are attribute references: in the expression modname.funcname, modname is a module object and funcname is an attribute of it. In this case there happens to be a straightforward mapping between the module’s attributes and the global names defined in the module: they share the same namespace!
任何‘.‘后的命名为称为属性.例如z.real,real是对象z的一个属性.严格地说,从模块中引用命名实际是属性引用:modname.funcname,modname是一个模块,funcname是它的一个属性.在这种情况下,在模块中定义的全局(global)命名和模块属性间有一个直接的映射,并且它们共用一个命名空间!
Attributes may be read-only or writable. In the latter case, assignment to attributes is possible. Module attributes are writable: you can write modname.the_answer = 42. Writable attributes may also be deleted with the del statement. For example, del modname.the_answer will remove the attribute the_answer from the object named by modname.
属性可能是只读或者可写的.后者情况下,对属性赋值是可能的.
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.)
命名空间在不同的时间被创建,并有不同生命周期.包含built-in命名的命名空间会在GIL启动的时候创建,而且永远不会被删除.对于模块的全局命名空间在模块的定义被读入时创建;通常,模块命名空间也生存到GIL退出前.无论从脚本文件或是交互的语句,在GIL的顶层调用执行的语句被认为是__main__模块的一部分,所以它们有它们自己的全局命名空间.
(*某些程序中:if __name__==__main__ 用来判断脚本文件是否是被GIL顶层调用,如果不是可以屏蔽一些不必要的操作,例如不必要的提示输出*)
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.
当函数被调用时它的本地or局部(local)命名空间被创建,当该函数返回(return)或抛出未被函数中处理的异常时命名空间被删除.实际上用遗忘来描述发生了什么更为贴切.当然,每个递归调用都有它们自己的本地命名空间.
以上是关于Python Scopes and Namespaces的主要内容,如果未能解决你的问题,请参考以下文章
Scala下Play框架学习笔记(Session and Flash scopes)
The confusion about jsp four scopes and ServletContext,HttpSession,HttpServletReqest,PageContext(示例代
redux 的 dispatch() 中的 [[Scopes]] 是啥
Blazor University (49)依赖注入 —— 比较依赖范围
解决:Specifying a namespace in include() without providing an app_name和XXX is not a registered namespa