[Tutor] variable scope in nested functions
Thanks Kent for your reply. You said This is a limitation of Python's nested scopes. You can't assign to a variable in an enclosing scope. One way to work around this is to use a mutable value like a list in the enclosing scope: def foo (n): counter = [0] def choose (i): if (solution found): counter[0] += 1 print counter[0], (solution) else choose (i+1) choose (1) I can't help thinking that this is a little unsatisfactory and suggests (with all due respect) an unfinished quality to Python. Consider a program I've just translated into python from my version in C. def erdos(n): #Explores Erdos theorem that any found, r, s = False, 1, [0]*1000#integer can be expressed as +-1.1 def choose (d, k): #+-2.2...+-r.r for some r and some for i in -1, 1: #combinations of +'s and -'s s[d] = i if d r: choose (d+1, k - i*d*d) elif k == i*d*d: found = True printout () def printout (): print n, '=', for i in range (1, r+1): if s[i] == 1: print '+', i,'.',i, else: print '-',i,'.',i, print '\n' while not found: choose(1, n) r += 1 The program is supposed to return to the python prompt as soon as it finds solution(s) at the smallest width r. I entered it exactly as it is expecting the same sort of error message for the boolean variable 'found'. There was none. Instead python simply fails to perform 'found = True' in choose() and it just keeps running (incidentally demonstrating the other part of this Erdos theorem that the no. can be so expressed in an infinite number of ways). Why the inconsistency in handling enclosing scope variables of type 'int' and 'bool'? Also, it can't be desirable that the interpreter effectively ignores a line of code without warning. Logesh ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] variable scope in nested functions
Hello list I am having trouble with a variable to act as a counter in a nested recursive function which will only occasionally find an answer. Something like a static variable in C. Why does this sort of thing not work? def foo (n): counter = 0 def choose (i): if (solution found): counter += 1 print counter, (solution) else choose (i+1) choose (1) I get an error message that counter is referenced before being declared. Declaring counter as global does not help either Incidentally how does one nest comments in python. I want to comment out part of a line. Is it just Kate my editor which won't allow it Thanks Logesh ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable scope in nested functions
Logesh Pillay wrote: Hello list I am having trouble with a variable to act as a counter in a nested recursive function which will only occasionally find an answer. Something like a static variable in C. Why does this sort of thing not work? def foo (n): counter = 0 def choose (i): if (solution found): counter += 1 print counter, (solution) else choose (i+1) choose (1) I get an error message that counter is referenced before being declared. This is a limitation of Python's nested scopes. You can't assign to a variable in an enclosing scope. One way to work around this is to use a mutable value like a list in the enclosing scope: def foo (n): counter = [0] def choose (i): if (solution found): counter[0] += 1 print counter[0], (solution) else choose (i+1) choose (1) You could also write a counter class: class foo: def __init__ (n): self.counter = 0 def choose (self, i): if (solution found): self.counter += 1 print self.counter, (solution) else self.choose (i+1) foo().choose (1) I would suggest separating the counter from the solution checking though. Declaring counter as global does not help either No, then it just looks for counter in the global (module) namespace so it still doesn't find it. Incidentally how does one nest comments in python. I want to comment out part of a line. Is it just Kate my editor which won't allow it Use # to comment out to the end of a line. There is no way to comment out the middle of a line (like you could do in C or Java with /* stuff */) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor