473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 938
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: zmcelrath87 | last post by:
I am having a problem involving the scope of timeouts and intervals. Since timeouts and intervals execute in the global scope, dynamically generated local interval/timeout declarations do not work,...
3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
188
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection -...
9
by: Phrogz | last post by:
I'm trying to write an 'each' function for a JavaScript array that behaves like Ruby's Array#each. (It doesn't matter if you know Ruby to help with this question.) My problem is the scope of...
6
by: Emanuele D'Arrigo | last post by:
Hi everybody, in the small snippet of code below, you can see two identical debugMsg(this.myname) statements, one correctly returns "manu" but the other returns "undefined". This is due to...
6
by: enaeher | last post by:
I would expect this code: globalFnArray = ; for (var i = 0; i < 5; i++) { globalFnArray.push (function () { alert (i) }); } for (var j = 0; j < 5; j++) { globalFnArray(); } to alert 0, 1, 2,...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
2
by: dhtmlkitchen | last post by:
I'm trying to learn something about JScript engine. I have the following example: <script>(function() { poop = "brown"; myFunction = function myFunction() {}
2
by: DanYan | last post by:
So I was doing some stuff in Javascript, and I want to get access to a function's scope chain. As a simplified example of what I actually am trying to do, suppose I have this: function add(b)...
2
by: Andrey Fedorov | last post by:
Is the scope of a closure accessible after it's been created? Is it safe against XSS to use closures to store "private" auth tokens? In particular, in... ....can untrusted code access...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.