On 9/10/07, Nikos Apostolakis <[EMAIL PROTECTED]> wrote: > Is it possible to have the variables that appear in "for" loops and > other such constructs to be "dummy" in the mathematical sence > (probably the cs term is "local")? The current behavior, in which > after the loop is completed the loop variable is _still_ set to the > last value it assumed, is very confusing -- at least to me. For > example, it took me some time to realize the mistake in the follow > sequence of commands: > > f(x) = sin(x)/x > for x in [1.0/2^i for i in range(1,15)]: > print x, f(x) > > for x in [-1.0/2^i for i in range(1,15)]: > print x, f(x) > > plot(f(x), -1, 1) > > I believe that a lot of mathematicians and students will be confused > by this behavior. At least for the Calculus part I think that it > would be a good idea to have a "mathematical for" in which the > looping variable is automatically reset after the completion of the > loop. IMHO, this is more in agreement with common mathematical > practice.
There is absolutely nothing that can be done in SAGE about this. It's a design decision that was made in the Python programming language, and we have to live with it so long as Python behaves this way. This is part of using a mainstream language instead of inventing our own. That said, I vaguely recall Guido van Rosum saying that he was seriously considering changing this behavior in "Python 3000", i.e., the version of Python that will seriously break backwards compatibility. I have no idea what will really happen. I think the reason Python behaves as it does now with respect to variables used in loops is that it is exactly how C behaves, or at least how C behaved when Python was first written in the early 1990's. I.e,. in (old) C you had: void foo() { declare *all* variables here for (n=0; n<10; n++) do something // now n is 10 } In Python that is most like this (since a C for loop is basically a while loop): sage: n=0 sage: while n < 10: ....: n += 1 ....: sage: print n 10 But it is not my place to try to justify the design decisions of Python from 15 years ago. On balance Python is an excellent highly readable language that people can learn quickly, and which has been used for an absolutely huge range of applications. It has really earned respect. -- William --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/ -~----------~----~----~----~------~----~------~--~---