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

Scope #30

Open
coleifer opened this issue Sep 22, 2016 · 0 comments
Open

Scope #30

coleifer opened this issue Sep 22, 2016 · 0 comments

Comments

@coleifer
Copy link

coleifer commented Sep 22, 2016

Here's one that blew my mind (and my co-workers):

def incrementer():
    i = 0
    def increment():
        i = i + 1
        return i
    return increment

Looks OK. Let's try it.

>>> i = incrementer()
>>> i()
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-3-fa1b63ea38ec> in <module>()
----> 1 i()

<ipython-input-1-482b5332e967> in increment()
      2         i = 0
      3         def increment():
----> 4                 i = i + 1
      5                 return i
      6         return increment

UnboundLocalError: local variable 'i' referenced before assignment

Fuck, even JavaScript got this one right!

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

No branches or pull requests

1 participant