How to do this in Python...

Steve Holden sholden at holdenweb.com
Tue Jan 28 08:54:17 EST 2003


"Alex Martelli" <aleax at aleax.it> wrote in message
news:rxsZ9.126669$AA2.5317615 at news2.tin.it...
> Cliff Wells wrote:
>    ...
> > Well, if conciseness is the goal, I'm shocked that no one has suggested
> > the obvious and practically idiomatic:
> >
> > if filter(None, [match for match in [re.search(pattern1, target)]]):
> >     print "1", match.string
> > elif filter(None, [match for match in [re.search(pattern2, target)]]):
> >     print "2", match.string
> > else:
> >     print "no match"
>
> Weird to use filter AND list comprehension when just the latter
> would suffice,,,:
>
> if [match for match in [re.search(pattern1,target)] if match]:
>
> etc.  The if clause in the list comprehension is useful here.
>
>
> Personally, I consider this idiom a (slight) _abuse_ of list
> comprehensions -- specifically, of the "implementation detail"
> that any identifier that gets bound in the for clause of a
> list comprehension gets bound in the SURROUNDING scope, i.e.,
> a list comprehension does not define a new nested scope.
>
> I guess that, if the idiom becomes a popular replacement for
> the missing assign-and-test, all future releases of Python
> will be practically constrained to let list comprehensions
> always "pollute" the surrounding scope in this way.  But this
> does not strike me, personally, as a very nice prospect anyway.
>
>

Unfortunately the developers have already backed away from making a list
comprehension use a private namespace for the bound variables, on the
grounds that there may already be software that takes advantage of the
pollution.

Personally I'm with you, and I think the bullet should be bitten
immediately. The longer it's left the more painful the transition will be.
But don't hold your breath. Python 3000, anyone?

regards
--
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Bring your musical instrument to PyCon!    http://www.python.org/pycon/







More information about the Python-list mailing list