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

Roel Schroeven roel at roelschroeven.net
Mon Feb 27 04:07:54 EST 2023


Op 27/02/2023 om 9:56 schreef inhahe:
> On Mon, Feb 27, 2023 at 3:52 AM Roel Schroeven <roel at roelschroeven.net>
> wrote:
>
> > 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.
> >
> > --
> >
> >
> I'm not sure, but I think I remember this was actually a bug in the
> interpreter, and presumably they didn't fix it because they didn't want to
> break backward compatibility?
I'm guessing you're thinking about variables leaking out of list 
comprehensions. I seem to remember (but I could be wrong) it was a 
design mistake rather than a bug in the code, but in any case it's been 
fixed now (in the 2 to 3 transition, I think).

For loops (and while loops, and if statements) not introducing a new 
scope is a deliberate decision and is not subject to change.

-- 
"Ever since I learned about confirmation bias, I've been seeing
it everywhere."
         -- Jon Ronson



More information about the Python-list mailing list