简体   繁体   English

运行python doc示例无法理解python作用域和名称空间一章中的内容?

[英]running python doc example has don't understand something in python scope and namespace chapter?

I learning python3.6 document, and when I look at python scopes and namespaces , I'm try running this code, i found in scope_test() call do_local() print result is different with me thought : 我正在学习python3.6文档,当我查看python scopes and namespaces ,我尝试运行此代码,我发现在scope_test()调用do_local()打印结果与我的想法不同:

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)

I think when call do_local() should be find spam in do_local() scope, because of do_local() scope has spam variable, so: 我认为在调用do_local()时应在do_local()范围内查找垃圾邮件,因为do_local()范围具有spam变量,因此:

do_local()
print('After local assignment:', spam) # local spam

unless do_local() not has spam variable, and then can find spam in scope_test() 除非do_local()没有spam变量,然后可以在scope_test()找到spam

but python Interpreter print result is: 但是python解释器的打印结果是:

do_local()
print('After local assignment:', spam) # test spam

do_local do_local

is similarly with when call do_global() , I think in this scope, bacause global spam is global, so: 与调用do_global()时类似,我认为在此范围内,因为global spam是全局的,所以:

do_global()
print("After global assignment:", spam) # test spam

but why result is: 但是为什么结果是:

do_global()
print("After global assignment:", spam) # nonlocal spam
def scope_test():
    def do_local():
        spam = "local spam" #this spam variable is in do_local's scope
    def do_nonlocal():
        nonlocal spam #this spam variable refers to spam variable defined in outer function's ,that is scope_test's,scope
        spam = "nonlocal spam"
    def do_global():
        global spam #this refers spam variable defined outside all functions (global)
        spam = "global spam"

    spam = "test spam" #this spam variable is defined in scope_test's scope

    do_local()
    print("After local assignment:", spam) #this spam var is still in scope_test's scope

    do_nonlocal() #do_nonlocal is called and inside that spam variable (defined nonlocal) is changed which changes scope_test's spam
    print("After nonlocal assignment:", spam) #this print is also in scop_test's scope but above function changed current scope's value 

    do_global() #changes spam var in global scope 
    print("After global assignment:", spam) #prints spam var in scop_test

scope_test() #execution starts
print("In global scope:", spam) #prints spam var in global scope as this print statement is in global scope

Here is the result I got after running your program. 这是我运行程序后得到的结果。 Let's go through it. 让我们经历一下。

def do_local():
    spam = "local spam"

An object spam is created which only has a scope limited to this function do_local and it get's destroyed as soon as the life of function ends. 将创建对象垃圾邮件,该垃圾邮件的范围仅限于此函数do_local,并且在函数寿命到期时将其销毁。 Hence, after local assignment, you see the output of test spam, which was defined using spam = "test spam" which has a scope pertaining to function scope_test. 因此,在本地分配后,您会看到测试垃圾邮件的输出,该输出是使用spam = "test spam"定义的,该spam = "test spam"的范围与函数scope_test有关。

def do_nonlocal():
    nonlocal spam
    spam = "nonlocal spam"

By using the nonlocal keyword you're telling the interpreter that you're working with an object that is not limited to the scope of function do_nonlocal . 通过使用nonlocal关键字,您将告诉解释器您正在使用的对象不限于功能do_nonlocal的范围。 Hence, the interpreter searches for this object outside this function's scope and it finds it in the function scope_test . 因此,解释器在此函数范围之外搜索该对象,并在函数scope_test找到它。 It then assigns the value of "nonlocal spam" to this spam object pertaining to the scope of function scope_test . 然后,它将与功能scope_test的范围相关的“非本地垃圾邮件”的值分配给该垃圾邮件对象。

def do_global():
    global spam
    spam = "global spam"

Here you're actually creating a global object spam and assigning it the value of "global spam". 在这里,您实际上是在创建全局对象垃圾邮件,并为其分配“全局垃圾邮件”的值。 Remember: This is global to the scope of your whole program. 请记住:这是整个程序范围的全局范围。 But when you print out spam, python looks for spam object and finds it in your function's scope only, ie spam which was allocated a value of "nonlocal spam" in do_nonlocal function call, hence you see nonlocal spam as your output. 但是,当您打印出垃圾邮件时,python会查找垃圾邮件对象并仅在函数范围内找到它,例如,在do_nonlocal函数调用中分配了“非本地垃圾邮件”值的垃圾邮件,因此您将非本地垃圾邮件视为输出。

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

After running scope test, the spam object's scope which contained the value of "test spam" initially, persists to exist after function scope_test is over. 运行范围测试后,最初包含“测试垃圾邮件”值的垃圾邮件对象的范围在函数scope_test结束后仍然存在。 Hence when you try to print out spam again, the global object spam gets printed out which you created when you called out do_global while in you were in test_scope . 因此,当您尝试再次打印出垃圾邮件时,全局对象垃圾邮件将被打印出来,该垃圾邮件是您在do_global中调用do_globaltest_scope


I hope it clears out some confusion from your mind! 我希望它能清除您的困惑!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM