Beforeword: in the last couple of months this answer has been downvoted considerably. I apology if my words might seem a bit rough, but I insist globals are really harmful, as explained in the documentation linked below. If you consider downvoting further down this answer, please read the documentation and elaborate why you disagree with what's below.
To quote an answer linked below: "The reason they are bad is that they allow functions to have hidden (non-obvious, surprising, hard-to-detect) side effects, leading to an increase in complexity, potentially leading to Spaghetti code."
- Using globals is very likely to mean wrong engineering. If you need a global, that means you need to redesign your code. That's even more true in python.
- when you do really want to use a global (maybe the only acceptable case: singleton, though in python, you'd only scope the singleton more globally than where you use it...), you need to declare your variable as global, and then attribute it a value.
For example:
global bar
bar = []
def foobar():
bar.append('X')
RTFM:
about the IPython part, my example does work:
In [1]: global bar
In [2]: bar = []
In [3]: def foo():
...: bar.append(3)
...:
In [4]: foo()
In [5]: foo()
In [6]: foo()
In [7]: bar
Out[7]: [3, 3, 3]
and here is another example, that shows global is indeed working, and not the outer scoping:
In [2]: def foo():
...: global bar
...: bar = []
...:
In [3]: def oof():
...: bar.append('x')
...:
In [4]: foo()
In [5]: oof()
In [6]: oof()
In [7]: oof()
In [8]: oof()
In [9]: bar
Out[9]: ['x', 'x', 'x', 'x']
anyway, globals are evil!