471,354 Members | 2,062 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Another dumb scope question for a closure.

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
Jan 9 '08 #1
2 879
On Jan 9, 11:47 am, "Steven W. Orr" <ste...@syslang.netwrote:
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 inc(jj):
.... def dummy():
.... dummy.jj += 1
.... return dummy.jj
.... dummy.jj = jj
.... return dummy
....
>>h = inc(33)
h()
34
>>h()
35
>>i = inc(12)
i()
13
>>i()
14

Waldemar
Jan 9 '08 #2
On Jan 9, 3:52 pm, Waldemar Osuch <waldemar.os...@gmail.comwrote:
On Jan 9, 11:47 am, "Steven W. Orr" <ste...@syslang.netwrote:
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 inc(jj):

... def dummy():
... dummy.jj += 1
... return dummy.jj
... dummy.jj = jj
... return dummy
...>>h = inc(33)
>h()
34
>h()
35
>i = inc(12)
i()
13
>i()

14

Waldemar
Here it is:
http://aspn.activestate.com/ASPN/Coo.../Recipe/474122
Jan 9 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Grant Wagner | last post: by
188 posts views Thread by christopher diggins | last post: by
9 posts views Thread by Phrogz | last post: by
6 posts views Thread by Emanuele D'Arrigo | last post: by
6 posts views Thread by enaeher | last post: by
2 posts views Thread by dhtmlkitchen | last post: by
2 posts views Thread by Andrey Fedorov | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.