Python 命名空间

Python namespaces

    A namespace is a mapping from names to objects.

 分类:

(1) The set of built-in names (containing functions such as abs(), and built-in exception names);

(2) The global names in a module;

(3) The local names in a function invocation.

(4) In a sense the set of attributes of an object.


生命周期:

(1) The namespace containing the built-in names is created when the Python interpreter starts up, and is never deleted.

(2) 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.)

(3) 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.


搜索顺序: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
python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量
This is an example demonstrating how to reference the different scopes and namespaces, and how global and nonlocal affect variable binding:
def scope_test():
    def do_local():
        spam = "local spam"
    def do_nonlocal():
        nonlocal spam
        spam = "nonlocal spam"
    def do_global():
        global spam
        spam = "global spam"
    spam = "test spam"
    
    do_local()
    print("After local assignment:", spam)
    do_nonlocal()
    print("After nonlocal assignment:", spam)
    do_global()
    print("After global assignment:", spam)


scope_test()
print("In global scope:", spam)

The output of the example code is:
After local assignment: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
In global scope: global spam


Note how the local assignment (which is default) didn’t change scope_test‘s binding of spam. The nonlocal assignment changedscope_test‘s binding of spam, and the global assignment changed the module-level binding.

You can also see that there was no previous binding for spam before the global assignment.



9.3. A First Look at Classes


9.3.1.  Class Definition Syntax

The simplest form of class definition looks like this:

class ClassName:
    <statement-1>
    .
    .
    .
    <statement-N>

When a class definition is entered, a new namespace is created, and used as the local scope — thus, all assignments to local variables go into this new namespace. In particular, function definitions bind the name of the new function here.

When a class definition is left normally (via the end), a class object is created. This is basically a wrapper around the contents of the namespace created by the class definition; The original local scope (the one in effect just before the class definition was entered) is reinstated, and the class object is bound here to the class name given in the class definition header (ClassName in the example).


9.3.2. Class Objects

Class objects support two kinds of operations: attribute references and instantiation.






global 关键字:
global关键字用来在函数或其他局部作用域中使用全局变量。但是如果不修改全局变量也可以不使用global关键字。
gcount = 0

def global_test():
    print (gcount)
    
def global_counter():
    global gcount
    gcount +=1
    return gcount
    
def global_counter_test():
    print(global_counter())
    print(global_counter())
    print(global_counter())

Output:
1
2
3

nonlocal 关键字:

nonlocal关键字用来在函数或其他作用域中使用外层(非全局)变量。
def make_counter():
    count = 0
    def counter():
        nonlocal count
        count += 1
        return count
    return counter


def make_counter_test():
    mc = make_counter()
    print(mc())
    print(mc())
    print(mc())


if __name__ == '__main__':
    make_counter_test()


Output:

1
2
3






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值