it seems like a few weeks ago... but actually it was more like 30 years ago that i was programming in C, and

avi.e.gross at gmail.com avi.e.gross at gmail.com
Mon Feb 27 13:40:42 EST 2023


I am not a big fan of religions or philosophies that say a road to salvation is for the "I" to disappear.

But on a more serious note, as Roel said, there is NO RULE being violated unless the documentation of the language says it is supposed to do something different.

There are many excellent reasons to keep the final value of a loop variable around. On the other hand, there are also many good reasons to make such variables be totally kept within the context of the loop so they can mask a variable with the same name only temporarily within the loop.

Neither choice is wrong as long as it is applied consistently.

Now, having said that, does python allow you to in some way over-ride the behavior?

Well, first, you can simply choose an odd name like local______loopy___variable that is not used elsewhere in your code, except perhaps in the next loop downstream where it is re-initialized.

You can also use "del Variable" or reset it to null or something in every way you can exit the loop such as before a break or in an "else" clause if it bothers you.

inhahe <inhahe at gmail.com> made the point that this may not have been the original intent for python and may be a sort of bug that it is too late to fix. Perhaps so, but insisting it be changed now is far from a simple request as I bet some people depend on the feature. True, it could be straightforward to recode any existing loops to update a secondary variable at the top of each loop that is declared before the loop and persists after the loop. 

Alas, that might force some to use the dreaded semicolon!

Final note is to look at something like the "with" statement in python as a context manager where it normally allows the resource to be closed or removed at the end. Of course you can set up an object that does not do the expected closure and preserves something, but generally what is wanted is to make sure the context exits gracefully.

Avi

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of Roel Schroeven
Sent: Monday, February 27, 2023 3:51 AM
To: python-list at python.org
Subject: Re: it seems like a few weeks ago... but actually it was more like 30 years ago that i was programming in C, and

Op 26/02/2023 om 6:53 schreef Hen Hanna:
> > There are some similarities between Python and Lisp-family 
> > languages, but really Python is its own thing.
>
>
>     Scope (and extent ?) of   variables is one reminder that  Python is not Lisp
>
>      for    i     in      range(5):      print( i )
>                       .........
>      print( i )
>
> ideally, after the FOR loop is done,  the (local) var  i should also disappear.
> (this almost caused a bug for me)
I wouldn't say "i *should* also disappear". There is no big book of programming language design with rules like that that all languages have to follow. Different languages have different behavior. In some languages, for/if/while statements introduce a new scope, in other languages they don't. In Python, they don't. I won't say one is better than the other; they're just different.

--
"Most of us, when all is said and done, like what we like and make up reasons for it afterwards."
         -- Soren F. Petersen

--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list