Hi everyone
I'm new to Python, so forgive me if the solution to my question should
have been obvious.
...
Good question. For a thorough explanation see: http://www.python.org/dev/doc/devel/ref/naming.html
Simple version follows:
OK, here's my problem: How do I best store and change lastX, A(lastX) and B(lastX) in FW's scope? This seems like it should be easy, but I'm stuck. Any help would be appreciated!
Assignments (i.e., binding names to objects) are always made in the local scope (unless you've used the 'global' declaration, which I don't think can help you here). So, for an even simpler demonstration of the problem see:
>>> def outer(): ... b = 1 ... def inner(): ... b += 1 ... print b ... inner() ... >>> outer() Traceback (most recent call last): File "<input>", line 1, in ? File "<input>", line 6, in outer File "<input>", line 4, in inner UnboundLocalError: local variable 'b' referenced before assignment
The solution is not to re-bind the identifier from the enclosing scope, but rather to mutate the object that it references. This requires a mutable object, such as a list:
>>> def outer(): ... b = [1] # bind b to a mutable object ... def inner(): ... b[0] += 1 ... print b[0] ... inner() ... >>> outer() 2 >>>
HTH Michael
-- http://mail.python.org/mailman/listinfo/python-list