python tutorial 学习笔记 (五) scope,namespace

Namespace

A mapping from name to objects. Names in different namespaces have no relation at all. Most namespaces are currently implemented as Python dictionaries e.g.: the set of built-in names; global names in a module; local names in a function invocation; in a sense, the attributes of an object

Namespaces are created at different moments and have different lifetimes.

Attribute

Any name following a dot. modules’s attribute    vs.     global names in the module     : they are not the same, but they share the same namespace. exception is __dict__ attribute is not a global name in that module.

Attibutes may be read-only or writable. Writable attibutes can be assigned value or be delted by “del” keyword.

Scope

A scope is a textual region of the Python program when a namespace is directly accessible. i.e.: don’t need dot. At least three nested scopes whose namespaces are ditrectly accessible:

  • the innter most scope( the namespaces of any enclosing function);
  • middle scope: current module’s global names
  • outermost scope: the namespace containing built-in names.

If a name is declared global, then all references go directly to the middle scope containing the module’s global names.  Otherwise, all variables found outside of the innermose scope are read-only.  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.

**[博主按]: And there is a scope that can be imported from module. When there’s confliction between names imorted from scope, the reference will go to the last imported one.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值