Aug-06-2018, 05:05 PM
(This post was last modified: Aug-06-2018, 05:06 PM by yksingh1097.)
(Aug-06-2018, 02:24 PM)Windspar Wrote: Because scope_test already has local variable name spam.
scope looks up local spam first. If not exist then looks for global spam.
spam = "global" def global_spam(): print(spam) def local_spam(): spam = "local" print(spam) local_spam() global_spam()
Hi Windspar,
Thanks for your explanation,but let me be more specific about my problem
I know that the scoping in python works by LEGB rule,
But when do_global() is called, control finds that do_global() is creating a global variable spam, which is priorly created in scope_test(). So why the variable spam in scope_test is not get overridden by spam variable of do_global(), as it happened when we called do_nonlocal() function (there value of spam = "test spam" became spam = "nonlocal spam",as you can see in the output)