Message213520
Inside functions:
1. Python allows access to global variables.
2. Python allows creation of local variable with the same name as that of of some of the global variable.
Keeping the above two statements in mind, I believe that Python must allow following sequential statements in a function:
1. Accessing global variable
2. Creating local variable with the same name as that of some of the global variable name.
But, it seems that the above is not allowed.
The below code has the following output:
Printing: 12
Throwing Error:
Traceback (most recent call last):
File "./bug.py", line 14, in <module>
func2()
File "./bug.py", line 9, in func2
print 'Throwing Error: ', var
UnboundLocalError: local variable 'var' referenced before assignment
CODE:
var = 12
def func1():
print 'Printing: ', var
def func2():
print 'Throwing Error: ', var
var = 10
print 'Unreachable Code: ', var
func1()
func2() |
|
Date |
User |
Action |
Args |
2014-03-14 07:23:36 | vijay_kansal | set | recipients:
+ vijay_kansal |
2014-03-14 07:23:36 | vijay_kansal | set | messageid: <1394781816.56.0.360131329411.issue20922@psf.upfronthosting.co.za> |
2014-03-14 07:23:36 | vijay_kansal | link | issue20922 messages |
2014-03-14 07:23:36 | vijay_kansal | create | |
|