[ python-Bugs-972467 ] local variables problem

SourceForge.net noreply at sourceforge.net
Mon Jun 14 04:36:28 EDT 2004


Bugs item #972467, was opened at 2004-06-14 04:20
Message generated for change (Comment added) made by tim_one
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=972467&group_id=5470

Category: None
>Group: Not a Bug
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Toon Verstraelen (tovrstra)
Assigned to: Nobody/Anonymous (nobody)
Summary: local variables problem

Initial Comment:
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


----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2004-06-14 04:36

Message:
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.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=972467&group_id=5470



More information about the Python-bugs-list mailing list