PEP 312 - Making lambdas implicit worries me, surely it's just the name 'lambda' that is bad...

Dave Brueck dave at pythonapocrypha.com
Mon Mar 3 19:33:00 EST 2003


On Mon, 3 Mar 2003, Alexander Schmolck wrote:

> Dave Brueck <dave at pythonapocrypha.com> writes:
>
> > > I don't think that follows. Python's list comprehensions are different -- they
> > > pollute the namespace.
> >
> > Oh, c'mon. This is a frequently cited yet overstated wart. Yeah,
> > technically it's a problem, but much more a theoretical than practical one
>
> It isn't a major problem, but I can remember that it did (infrequently) cause
> me bugs, so it's not all theoretical.

Could you post an example? Maybe I'm just being too narrowminded, but the
cases where it causes problems seem to be bad coding style anyway. I keep
thinking of stuff like:

x = 5
y = [int(x) for x in seq]
z = x + 2

Relying on x to still be 5 by the time z uses it, or relying on the value
of x to be some value because of what happened in the list comp both seem
sloppy to me. Am I missing some other common scenario?

> Another minor cause of annoyance are things like this:
>
>   SOME_GLOBAL_CONST = [(y**2, x**2) for (x,y) in SOME_OTHER_GLOBAL_CONST]
>   del x, y # I could do without this

I _do_ do without it, and it doesn't cause any problems.

> > - in practice the only time it causes problems is when you're doing
> > something questionable anyway, like reusing a variable without
> > initializing it to some known value.
>
> Or accidently overwriting a variable (or modulename).

Perhaps so. I guess I just reacted because I've never seen it happen in
practice. It seems like it'd be pretty rare in most cases, assuming
meaningful variable names. It's not too hard to dream up a counterexample,
but all the counterexamples I dreamt up _seemed_ dreamt up rather than
real ones. :)

> All this might not amount to much, but I do think they violate the spirit of
> functional programming enough to bias some people who like FP towards map and
> filter.

If you say so... do you explicitly delete your for-loop variables too?

for i in range(5):
  print i
del i

If namespace pollution is a problem for you with listcomps, how are you
able to avoid it with for-loops? Might not your same problem avoidance
techniques apply to both?

Nice talking to you,
Dave





More information about the Python-list mailing list