Aug-06-2018, 10:26 AM
(This post was last modified: Aug-06-2018, 10:26 AM by yksingh1097.)
I was reading Python docs and encountered this example under "Scope and Namespaces".
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 to this code is
Output:After local assignment: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
In global scope: global spam
My question is, why in output, it isOutput:After global assignment: nonlocal spam
according to me it should be Output:After global assignment: global spam
as spam become a global variable after calling do_global(), and value becomes spam = "global spam"