davidbrochart / akernel

Asynchronous Python Jupyter kernel
MIT License
72 stars 5 forks source link

Global issues #4

Closed jasongrout closed 3 years ago

jasongrout commented 3 years ago

I'm curious how this interacts with global variables inside a function inside a cell, for example:

a = 1

def f():
    global a
    a = 5

f()
print(a)

This prints out 5 with the current ipykernel, which is what I would expect.

davidbrochart commented 3 years ago

With akernel this prints out 1, unless:

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)

def f(): nonlocal a a = 5

f() print(a)

davidbrochart commented 3 years ago

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 !