Another dumb scope question for a closure.

S

Steven W. Orr

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
 
W

Waldemar Osuch

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?

I have seen this approach on ActiveState Cookbook but can not find a
reference to it right now.
.... def dummy():
.... dummy.jj += 1
.... return dummy.jj
.... dummy.jj = jj
.... return dummy
....14

Waldemar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top