Another dumb scope question for a closure.

Steven W. Orr steveo at syslang.net
Wed Jan 9 13:47:30 EST 2008


So sorry because I know I'm doing something wrong.

574 > cat c2.py
#! /usr/local/bin/python2.4

def inc(jj):
     def dummy():
         jj = jj + 1
         return jj
     return dummy

h = inc(33)
print 'h() = ', h()
575 > c2.py
h() =
Traceback (most recent call last):
   File "./c2.py", line 10, in ?
     print 'h() = ', h()
   File "./c2.py", line 5, in dummy
     jj = jj + 1
UnboundLocalError: local variable 'jj' referenced before assignment

I could have sworn I was allowed to do this. How do I fix it?

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net



More information about the Python-list mailing list