This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vijay_kansal
Recipients vijay_kansal
Date 2014-03-14.07:23:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1394781816.56.0.360131329411.issue20922@psf.upfronthosting.co.za>
In-reply-to
Content
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()
History
Date User Action Args
2014-03-14 07:23:36vijay_kansalsetrecipients: + vijay_kansal
2014-03-14 07:23:36vijay_kansalsetmessageid: <1394781816.56.0.360131329411.issue20922@psf.upfronthosting.co.za>
2014-03-14 07:23:36vijay_kansallinkissue20922 messages
2014-03-14 07:23:36vijay_kansalcreate