M
Matt Knepley
I must be misunderstanding how Python 2.3 handles lexical scoping.
Here is a sample piece of code:
def run():
a = 1
def run2(b):
print a
run2(2)
print a
run()
which gives the output:
1
1
whereas this piece of code:
def run():
a = 1
def run2(b):
a = b
print a
run2(2)
print a
run()
gives:
2
1
and finally this code bombs:
def run():
a = 1
def run2(b):
print a
a = b
run2(2)
print a
run()
with an error about UnboundLocal. It seems that lexical scope works
only for references, and as soon as I make an assignment a new local
is created. Is this true?
Matt
Here is a sample piece of code:
def run():
a = 1
def run2(b):
print a
run2(2)
print a
run()
which gives the output:
1
1
whereas this piece of code:
def run():
a = 1
def run2(b):
a = b
print a
run2(2)
print a
run()
gives:
2
1
and finally this code bombs:
def run():
a = 1
def run2(b):
print a
a = b
run2(2)
print a
run()
with an error about UnboundLocal. It seems that lexical scope works
only for references, and as soon as I make an assignment a new local
is created. Is this true?
Matt