Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global issues #4

Closed
jasongrout opened this issue Jun 29, 2021 · 2 comments · Fixed by #5
Closed

Global issues #4

jasongrout opened this issue Jun 29, 2021 · 2 comments · Fixed by #5

Comments

@jasongrout
Copy link

jasongrout commented Jun 29, 2021

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
Copy link
Owner

davidbrochart commented Jun 29, 2021

With akernel this prints out 1, unless:

  • we also define 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:
# cell 1
a = 1
# cell 2
def f():
    global a
    a = 5

f()
print(a)
  • or we use nonlocal instead of global:
a = 1

def f():
    nonlocal a
    a = 5

f()
print(a)

@davidbrochart
Copy link
Owner

davidbrochart commented Jun 29, 2021

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 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants