Back to Community
augmented dictionary 'context' in notebook?

That context dot notation is pretty cool for creating global variables. Is there a way to create the same functionality in a notebook? I see context is an 'augmented dictionary' and I tried to initialize(context) in a notebook but that failed.

As a parallel ?, what is the 'pythonic' way to create and use global variables in a notebook (noting that if you assign to it in a function, it loses it's globality)?

Example 1:

foo = 2

def test():  
    print foo      # Crash, local referenced before assignment  
    foo = 3  

Example 2:

foo = 2  
def test(foo):  
    foo = 3  
    return foo

foo = test(foo)

works but seems like there s/b a better way...

Thanks for looking!

1 response

derr, that's a class!

class my_stuff:  
    foo = 2  
gbl = my_stuff

def test():  
    print gbl.foo  
    gbl.foo = 3

print gbl.foo  
test()  
print gbl.foo  

2
2
3