Skip to content

Commit

Permalink
#193 The out of scope variable, nonlocal (#228)
Browse files Browse the repository at this point in the history
* fixed link to CONTRIBUTORS.md

* The out of scope variable, nonlocal, #193

* added myself to CONTRIBUTORS.md :D

* Update CONTRIBUTORS.md

* added entry in index

* merged nonlocal to `the out of scope variable` example

Also removed the extra entry from the main index.
  • Loading branch information
diptangsu authored Oct 5, 2020
1 parent 9b3f869 commit 7525e80
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
3 changes: 1 addition & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ Few things that you can consider while writing an example,
- If you are choosing to submit a new example without creating an issue and discussing, please check the project to make sure there aren't similar examples already.
- Try to be consistent with the namings and the values you use with the variables. For instance, most variable names in the project are along the lines of `some_string`, `some_list`, `some_dict`, etc. You'd see a lot of `x`s for single letter variable names, and `"wtf"` as values for strings. There's no strictly enforced scheme in the project as such, but you can take a glance at other examples to get a gist.
- Try to be as creative as possible to add that element of "surprise" in the setting up part of an example. Sometimes this may mean writing a snippet a sane programmer would never write.
- Also, feel free to add your name to the [contributors list](/CONTRIBUTING.md).

- Also, feel free to add your name to the [contributors list](/CONTRIBUTORS.md).
2 changes: 2 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Following are the wonderful people (in no specific order) who have contributed t
| koddo | [koddo](https://github.com/koddo) | [#80](https://github.com/satwikkansal/wtfpython/issues/80), [#73](https://github.com/satwikkansal/wtfpython/issues/73) |
| jab | [jab](https://github.com/jab) | [#77](https://github.com/satwikkansal/wtfpython/issues/77) |
| Jongy | [Jongy](https://github.com/Jongy) | [#208](https://github.com/satwikkansal/wtfpython/issues/208), [#210](https://github.com/satwikkansal/wtfpython/issues/210) |
| Diptangsu Goswami | [diptangsu](https://github.com/diptangsu) | [#193](https://github.com/satwikkansal/wtfpython/issues/193) |

---

**Translations**
Expand Down
44 changes: 41 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,8 @@ Even when the values of `x` were different in every iteration prior to appending

- When defining a function inside a loop that uses the loop variable in its body, the loop function's closure is bound to the variable, not its value. So all of the functions use the latest value assigned to the variable for computation.

- To get the desired behavior you can pass in the loop variable as a named variable to the function. **Why does this work?** Because this will define the variable again within the function's scope.
- To get the desired behavior you can pass in the loop variable as a named variable to the function. **Why does this work?** Because this will define the variable
within the function's scope.

```py
funcs = []
Expand Down Expand Up @@ -1994,6 +1995,20 @@ def some_func():
def another_func():
a += 1
return a


def some_closure_func():
a = 1
def some_inner_func():
return a
return some_inner_func()

def another_closure_func():
a = 1
def another_inner_func():
a += 1
return a
return another_inner_func()
```

**Output:**
Expand All @@ -2002,11 +2017,15 @@ def another_func():
1
>>> another_func()
UnboundLocalError: local variable 'a' referenced before assignment

>>> some_closure_func()
1
>>> another_closure_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_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.
* 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_func`, but it has not been initialized previously in the same scope, which throws an error.
* To modify the outer scope variable `a` in `another_func`, use `global` keyword.
```py
def another_func()
Expand All @@ -2020,6 +2039,25 @@ UnboundLocalError: local variable 'a' referenced before assignment
>>> another_func()
2
```
* In `another_closure_func`, `a` becomes local to the scope of `another_inner_func`, but it has not been initialized previously in the same scope, which is why it throws an error.
* 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
```
* The keywords `global` and `nonlocal` are ways to simply tell the python interpreter to not delcare new variables, but to just look them up from the corresponding scope.
* 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.

---

Expand Down

0 comments on commit 7525e80

Please sign in to comment.