Weird local variables behaviors

S

Sebastjan Trepca

Hey,

can someone please explain this behavior:

The code:

def test1(value=1):
def inner():
print value
inner()


def test2(value=2):
def inner():
value = value
inner()

test1()
test2()

[trepca@sauron ~/dev/tests]$ python locals.py
1
Traceback (most recent call last):
File "locals.py", line 13, in <module>
test2()
File "locals.py", line 10, in test2
inner()
File "locals.py", line 9, in inner
value = value
UnboundLocalError: local variable 'value' referenced before assignment

Why can't he find the variable in the second case?


Thanks, Sebastjan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top