Re:Just why did people start using Python anyway?
by EliAndrewC on Thursday October 09, 2008 @10:45AM
(#327857)
Python 3.0 fixes this with the nonlocal statement. However, you are correct that this doesn't work in Python 2.5 and earlier.
Re:Just why did people start using Python anyway?
by silentbicycle on Thursday October 09, 2008 @11:05AM
(#327879)
Ok, thanks.
Re:Just why did people start using Python anyway?
by anamax on Thursday October 09, 2008 @11:48AM
(#327927)
Argh - I can't get the formatting to work.
def makeAdder2(x):
v = [x,]
def inc(y):
v[0] += y
return v[0]
return inc
works just fine in all pythons with closures (2.0 and above?)def makeAdder2(x):
def inc(y, v=[x,]):
v[0] += y
return v[0]
return inc
Also works and provides a mechanism for resetting the state.
Re:Just why did people start using Python anyway?
by silentbicycle on Thursday October 09, 2008 @01:03PM
(#328017)
Interesting. So keeping a reference to a list with the value works, but the value directly doesn't? Thanks for the note.
Good to know it's possible, though I'd rather it handle closures / lexical scoping directly.
Re:Just why did people start using Python anyway?
by eru on Thursday October 09, 2008 @10:33AM
(#327840)
You may need to add a 'global' statement or something similiar. If you are going to mutate a variable in a function Python assumes that you want to create a new variable in that scope.
Generally I do not mutate variables as far as possible, so I have not hit this problem myself, yet. Perhaps there is someone more knowledgeable about Python to step in.
Re:Just why did people start using Python anyway?
by silentbicycle on Thursday October 09, 2008 @10:39AM
(#327853)
But it's not a global variable, it's scoped within the closure. So, this means Python doesn't have lexical scoping?
--
In Lua:
Works fine. (Also, the syntax is really clean.)