From dc7b16c0f3c0789f6b7ce9bc45167e918e245bec Mon Sep 17 00:00:00 2001 From: diptangsu Date: Fri, 2 Oct 2020 19:19:10 +0530 Subject: [PATCH] The out of scope variable, nonlocal, #193 --- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/README.md b/README.md index 9f30a41..ed7ffaf 100644 --- a/README.md +++ b/README.md @@ -2023,6 +2023,53 @@ UnboundLocalError: local variable 'a' referenced before assignment --- +### ▶ The out of scope variable (again?) + +```py +def some_func(): + a = 1 + def some_inner_func(): + return a + return some_inner_func() + +def another_func(): + a = 1 + def another_inner_func(): + a += 1 + return a + return another_inner_func() +``` + +**Output:** +```py +>>> some_func() +1 +>>> another_func() +UnboundLocalError: local variable 'a' referenced before assignment +``` + +#### 💡 Explanation: +* When you make an assignment to a variable in scope, it becomes local to that scope. So `a` becomes local to the scope of `another_inner_func` inside `another_func`, but it has not been initialized previously in the same scope, which throws an error. +* Read [this](http://sebastianraschka.com/Articles/2014_python_scope_and_namespaces.html) short but an awesome guide to learn more about how namespaces and scope resolution works in Python. +* To modify the outer scope variable `a` in `another_inner_func`, use the `nonlocal` keyword. +```py +def another_func(): + a = 1 + def another_inner_func(): + nonlocal a + a += 1 + return a + return another_inner_func() +``` + +**Output:** +```py +>>> another_func() +2 +``` + +--- + ### ▶ Deleting a list item while iterating ```py