On Sep 10, 2007, at 21:34 , William Stein wrote:
>
> 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.
Other than lots of code breaking :-}
> 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
> }
This is the way C is defined. I'd be surprised if it would change.
Justin
--
Justin C. Walker, Curmudgeon-At-Large
Director
Institute for the Enhancement of the Director's Income
--------
"Weaseling out of things is what separates us from the animals.
Well, except the weasel."
- Homer J Simpson
--------
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
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/
-~----------~----~----~----~------~----~------~--~---