[issue20922] Global variables

VIJAY KANSAL report at bugs.python.org
Fri Mar 14 08:23:36 CET 2014


New submission from VIJAY KANSAL:

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()

----------
messages: 213520
nosy: vijay_kansal
priority: normal
severity: normal
status: open
title: Global variables
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20922>
_______________________________________


More information about the Python-bugs-list mailing list