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.

classification
Title: local variables problem
Type: Stage:
Components: None Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: tim.peters, tovrstra
Priority: normal Keywords:

Created on 2004-06-14 08:20 by tovrstra, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg21183 - (view) Author: Toon Verstraelen (tovrstra) Date: 2004-06-14 08:20
def test1():
    print a
    a += 1
                                                      
                                                      
                                               
a = 1
test1()


This results in a UnboundLocalError, while a is already
assigned

Traceback (most recent call last):
  File "bug.py", line 6, in ?
    test1()
  File "bug.py", line 2, in test1
    print a
UnboundLocalError: local variable 'a' referenced before
assignment
msg21184 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-06-14 08:36
Logged In: YES 
user_id=31435

Not a bug.  Ask on comp.lang.python for an explanation, or 
read the docs more carefully.  Short course:  'a' is local in 
test1 because 'a' is assigned to in test1.  It has no relation 
to the global named 'a'.  If you want test1 to use the global 
named 'a', put

    global a

as the first line of test1.
History
Date User Action Args
2022-04-11 14:56:04adminsetgithub: 40397
2004-06-14 08:20:37tovrstracreate