好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python命名空间与作用域

Python进阶 - 命名空间与作用域 写在前面
def fun1():
    i = 1def fun2():
    i = 2 
Traceback (most recent call last):
  File "scope_test.py", line 15, in <module>
    print(i)NameError: name 'i' is not defined 
def try_to_define_name():    '''函数中定义了名字i,并绑定了一个整数对象1'''
    i = 1try_to_define_name()print(i) #引用名字i之前,调用了函数 
Traceback (most recent call last):
  File "scope_test.py", line 20, in <module>
    print(i) #引用名字i之前,调用了函数NameError: name 'i' is not defined 
if True:
    i = 1print(i) # output: 1,而不是NameError 
def import_sys():    '''import sys module'''
    import sys

import_sys()print(sys.path) # NameError: name 'sys' is not defined 
def low_scope():    print(s)

s = 'upper scope'low_scope() 
def low_scope():
    s = 'lower scope's = 'upper scope'low_scope()print(s) 
def low_scope():
    l[0] = 2l = [1, 2]
low_scope()print(l) # [2, 2] 
def low_scope():
    l = [2, 2]

l = [1, 2]
low_scope()print(l) # [1, 2] 
def scope_test():    
    def do_local():
        spam = 'local spam'

    def do_nonlocal():        nonlocal spam # 当外层作用域不存在spam名字时,nonlocal不能像global那样自作主张定义一个
        spam = 'nonlocal spam' # 自由名字spam经nonlocal声明后,可以做重绑定操作了,可写的。

    def do_global():        global spam # 即使全局作用域中没有名字spam的定义,这个语句也能在全局作用域定义名字spam
        spam = 'global spam' # 自有变量spam经global声明后,可以做重绑定操作了,可写的。

    spam = 'test spam'
    do_local()    print("After local assignment:", spam) # After local assignment: test spam
    do_nonlocal()    print("After nonlocal assignment:", spam) # After nonlocal assignment: nonlocal spam
    do_global()    print("After global assignment:", spam) # After global assignment: nonlocal spamscope_test()print("In global scope:", spam) # In global scope: global spam 
def nest_outter():
    spam = 'outer'

    def nest_inner():        nonlocal spam1
        spam1 = 'inner'

    nest_inner()    print(spam)

nest_outter() 
  File "scope_test.py", line 41
    nonlocal spam1SyntaxError: no binding for nonlocal 'spam1' found 
4. 一些坑
def test():    print(i)
    i = 1i = 2test() 

Output:

Traceback (most recent call last):
  File "scope_test.py", line 42, in <module>
    test()
  File "scope_test.py", line 38, in test
    print(i)
UnboundLocalError: local variable 'i' referenced before assignment 
class Test(object):

    i = 1

    def test_print(self):        print(i)

t = Test()
i = 2t.test_print() 
class Test(object):

    i = 1

    def test_print(self):        print(i)

t = Test()
i = 2# t.test_print()Test.test_print(t) # 方法调用最后转换成函数调用的方式 
class Test(object):

    a = 1
    b = [a + i for i in range(10)]    print(b)    def test(self):        pass 
Traceback (most recent call last):
  File "scope_test.py", line 15, in <module>
    class Test(object):
  File "scope_test.py", line 18, in Test
    b = [a + i for i in range(10)]
  File "scope_test.py", line 18, in <listcomp>
    b = [a + i for i in range(10)]NameError: name 'a' is not defined 
def test1():
    i = 1def test2():    print(i)

test1()
test2() 

期待在 test2 中访问 test1 的名字 i ,显然是不可行的。

参考

Python Scopes and Namespaces

Python命名空间和作用域窥探

Python的作用域

Naming and binding

以上就是Python命名空间与作用域的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于Python命名空间与作用域的详细内容...

  阅读:39次