Closed jasongrout closed 3 years ago
With akernel this prints out 1
, unless:
a
as global outside of the function:
global a
a = 1
def f(): global a a = 5
f() print(a)
- or the cell is divided in two:
```python
# cell 1
a = 1
# cell 2
def f():
global a
a = 5
f()
print(a)
nonlocal
instead of global
:
a = 1
def f(): nonlocal a a = 5
f() print(a)
I'm thinking we could parse the code and look for top-level assignments, and declare every assigned variable as global. Thanks for raising this issue @jasongrout !
I'm curious how this interacts with
global
variables inside a function inside a cell, for example:This prints out
5
with the current ipykernel, which is what I would expect.