[Tutor] Sometimes unreferenced!?

John Abbe johnca@ourpla.net
Thu Feb 6 02:06:22 2003


Okay, as suggested, i'm trying to modularize my code, and now Python 
is complaining about a variable being referenced before assignment, 
*but* only with some ways i reference it. To give a much-simplified 
but real example -- the code:

=================================
#!/usr/local/bin/python

groupData = [1, 2, 3]

def e():
    groupData.reverse()

def f():
    records = [4, 5]
    groupData += records

def main():
    print "Before e: %s\n\n" % groupData
    e()
    print "After e, Before f: %s\n\n" % groupData
    f()
    print "After f: %s\n" % groupData

if __name__ == '__main__':
    main()
=================================
[Unix prompt]> problem.py         [problem.py is the name of the above file]
Before e: [1, 2, 3]


After e, Before f: [3, 2, 1]


Traceback (most recent call last):
   File "./problem.py", line 20, in ?
     main()
   File "./problem.py", line 16, in main
     f()
   File "./problem.py", line 10, in f
     groupData += records
UnboundLocalError: local variable 'groupData' referenced before assignment
=================================

Help?

Life,
John
-- 
   ------===>>  AbbeNormal  <<===------  |            ..:::..
    A wiki-weblog, somewhere under the   |         .:::::::*:::.
      multi-dimensional normal curve     |      ..:::::::::::::::..
       http://ourpla.net/cgi/pikie       |....::::::::::*:::::::::::*....