Zen of Python

Tim Peters tim.peters at gmail.com
Sat Jan 22 23:31:43 EST 2005


[Tim Peters]
>> But at that time, Python didn't have lexical scoping, and it wasn't
>> clear that it ever would.  So what's the bigger wart?  Making
>> listcomps exactly equivalent to an easily-explained Python for-loop
>> nest, or introducing a notion of lexical scope unique to listcomps,
>> hard to explain in terms of the way the rest of the language worked?

[Paul Rubin]
> Oops, I'd gotten confused and thought lexical scope came first and
> listcomps afterwards.  If lexical scope came afterwards, then
> implementing listcomps as a for-loop at that time makes more sense.

Yes, listcomps were introduced in 2.0b1, lexical scoping in 2.1a2 (via
explict __future__ request; you couldn't use it freely before 2.2).

> Of course in that case, since the absence of lexical scope was a wart
> in its own right, fixing it had to have been on the radar.

It's unclear why you believe that.  There was almost no demand for
lexical scoping across the years before lambda was introduced.  After
lambda, abuses of the default-argument mechanism to carry bindings
into lambda bodies became rampant, and that was an ugliness nobody
much liked.  If a reasonable solution to that alone had been found
that didn't require lexical scoping, I expect that's what Python would
have wound up with instead.  The case for lexical scoping in Python
was never compelling, and Guido didn't do anything to add it except
(eventually & reluctantly) agree to stay out of Jeremy Hylton's (my
Scheme-head friend) way.  Without Jeremy's specific involvement, I
doubt we'd have lexical scoping today -- no other developer cared
enough to implement it.

I'm happy it got in, BTW!  It's a reasonably useful feature -- but I
was happy enough with Python's original 3-level scoping.

> So turning the persistent listcomp loop var into a documented feature, instead
> of describing it in the docs as a wart that shouldn't be relied on, wasn't such a
> hot idea.

It did no harm at the time.  Morons (hard to distinguish from "clever
programmers" at times) always rely on dubious implementation details,
and that won't change, documented or not.  The current docs say this
(language reference manual):

    In Python 2.3, a list comprehension "leaks" the control variables
of each "for"
    it contains into the containing scope. However, this behavior is deprecated,
    and relying on it will not work once this bug is fixed in a future release 

So they've been warned.

> Adding lexical scope and listcomps at the same time might have also been a
> good way to solve the issue.

Find a way to transform just a smidgen of your hindsight into
foresight, and you'll get soooooo rich soooooo fast <wink>.



More information about the Python-list mailing list