This is not so much a variable / immutable problem as one of the areas.
"b" exists only in bodies fun1 and fun2. It is absent on a major or global scale (at least intentionally)
- EDIT -
>>> def fun1(b): ... b = b + 1 ... return b ... >>> def fun2(a): ... b = 1 ... return b ... >>> fun1(5) 6 >>> fun2(b) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'b' is not defined
(From my interpreter in the terminal)
I assume that your 'b' was initialized somewhere else. What happened in another function does not affect this.
This is me using your exact code:
>>> main() [3, 2, 1] [3, 2, 1] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 5, in main NameError: global name 'b' is not defined >>> b = 'whatever' >>> main() [3, 2, 1] [3, 2, 1] [3, 2, 1] whatever
source share