Skip to content

Instantly share code, notes, and snippets.

@kevinclark
Created December 7, 2010 04:16
Show Gist options
  • Save kevinclark/731446 to your computer and use it in GitHub Desktop.
Save kevinclark/731446 to your computer and use it in GitHub Desktop.
>>> b = 0
>>> def incB():
... b += 1
...
>>> c = []
>>> def addC():
... c.append(1)
...
>>> b
0
>>> c
[]
>>> incB()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in incB
UnboundLocalError: local variable 'b' referenced before assignment
>>> addC()
>>> c
[1]
@kevinclark
Copy link
Author

Yep, the reason it happens makes sense. But it's the old 'const ptr vs ptr to const' issue. Seems like we should be able to avoid that by now in high level languages. I'd actually prefer if 'global' was required to access the external scope.

@gpshead
Copy link

gpshead commented Dec 7, 2010

Heh I wouldn't mind that either since I absolutely hate globals in code that are not constants declared in ALL_CAPS. I really doubt that'd fly if suggested on python-ideas@ though. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment