2

I have nested a function definition bar() inside another function foo(). Now I am trying to access a variable located in the outer function foo() from the nested function bar(). This however doesn't work because of the scoping rules (see error traceback below).

I am looking for something similar to the global keyword, which however only enables me to access global variables, whereas this is some kind of semi-global variable.

Here's example code:

def foo():
    i = 0
    def bar():
        # how can I get access to the variable from enclosing scope?
        i += 1
    bar()

foo()

The output is:

$ python test.py
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    foo()
  File "test.py", line 5, in foo
    bar()
  File "test.py", line 4, in bar
    i += 1
UnboundLocalError: local variable 'i' referenced before assignment
2
  • what are you trying to achieve? Commented Apr 17, 2012 at 15:05
  • Please explain the reason for doing it. This will help us to give better solutions Commented Apr 17, 2012 at 15:06

2 Answers 2

8

You need the nonlocal statement instead of global.

i is clearly not global, but it is also not local to foo. It is local to __init__. Thus, in order to access it, declare it nonlocal.

Unfortunately, nonlocal ist python3 only. You can simulate it via closure, but that'd get pretty ugly.

Sign up to request clarification or add additional context in comments.

1 Comment

apparantly nonlocal is only available for Python 3.x , but this was a great search keyword.
3

Here is your work around:

class Test(object):
    def __init__(self):
        i = [0]
        def foo():
            i[0] += 1
        foo()
        print i[0]

t = Test()

This would be a use case for the nonlocal keyword, instead of global but that is only available as of Python 3.

3 Comments

+1 for the Python 2.x workaround. Surprisingly container types are unaffected from this.
They are affected. What saves you here, is that the scoping rules DO allow READ access. Technically, you do not WRITE to i, you write to i[0]...
Interesting, found this answer to explain this very well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.