colstrom / tinypy

A very small Python
Other
1 stars 0 forks source link

var changes from global to local scope within function #16

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Run the following code:

def foo():
#    global v
    print("v=", v)
    v = 0
    print("v=", v)

v = 42
foo()
print("v=", v)

What is the expected output? What do you see instead?

Python 2.5 throws "UnboundLocalError: local variable 'v' referenced before 
assignment" due to the first line of code in foo.  Instead, tinypy finds 
the variable in the global namespace eventhough the global keyword is not 
used.

Please use labels and text to provide additional information.

Original issue reported on code.google.com by dwhall...@gmail.com on 4 Jun 2008 at 3:42

GoogleCodeExporter commented 9 years ago
It is actually proper for v to be discovered by a function if it is only 
"read-only"
 In this case, it finds it (for reading) but when it goes to write to it, it changes
the variable use to a local variable.  During that change it should notice and 
raise
an exception.

I've changed the summary to reflect this a bit better.

Original comment by philhas...@gmail.com on 4 Jun 2008 at 3:47

GoogleCodeExporter commented 9 years ago
Actually, what Python seems to be doing is a bit more complex. It seems to be 
looking
ahead into the function to see if v gets written to anywhere inside it and if it
does, it raises an exception on the first read (the first print) and NOT the 
first write.

Original comment by denis.ka...@gmail.com on 13 Jun 2008 at 3:16

GoogleCodeExporter commented 9 years ago

Original comment by philhas...@gmail.com on 5 Sep 2008 at 5:15

GoogleCodeExporter commented 9 years ago

Original comment by philhas...@gmail.com on 6 Sep 2008 at 8:02