On Sat, 5 Mar 2022 at 12:12, Chris Angelico <ros...@gmail.com> wrote:
>
> On Sat, 5 Mar 2022 at 22:22, Stephen J. Turnbull
> <stephenjturnb...@gmail.com> wrote:
>
> > Python has one, and you've already mentioned it:
> >
> >     for thing in (x for x in this_collection if is_interesting(x)):
> >
> > It's noticably verbose, but it's an exact translation of your
> > statement of the abstract concept above.  It has all the benefits of
> > the proposed syntax except compactness[1].
>
> It is extremely verbose, considering that the filtering part of a
> comprehension uses the same variable name as iteration, doesn't need
> anything to be repeated, and is just another clause. A simple
> representation of that in a statement loop would be "for thing in
> this_collection if is_interesting(thing):", which doesn't repeat
> itself at all (the variable name "thing" is kinda essential to the
> concept of filtration here, so I don't count that); and repetition
> isn't simply about number of characters on the line, it's about
> reducing the potential for errors. (Plus, the genexp adds a
> significant amount of run-time overhead.)
>
> So you're right, I stand (partly) corrected: there IS a very clunky
> way to spell this concept.
>
> What Python needs is a non-clunky way to express this.

for thing in filter(is_interesting, this_collection):
    ...

That seems pretty non-clunky. Is the issue here that "filter" is not
sufficiently well-known? Or that you don't want to name the
is_interesting function, and lambdas are "too clunky"? This feels like
another case where the general dislike of lambda results in people
wanting special-case syntax so they can avoid either writing a
throwaway function, or using lambda.

If we had "placeholder" expressions (see, for example
https://pypi.org/project/placeholder/) so we could do things like

from placeholder import _
for thing in filter(_%3==0, the_list):
    ...

would that be sufficiently "non-clunky"?

There are many ways of achieving this sort of result. Clearly, from
the fact that this request comes up repeatedly, there's *something*
unsatisfying about all of them, but I'm not entirely clear what
particular problem is uniquely solved by new "for x in collection if
condition" syntax, and not by any of the other possibilities?

Personally, I don't mind having the if on a separate line, I find the
generator expression tolerable but a bit verbose, and I'm glad
"filter" and the placeholder library exist in case I need them, but
I've never really found the existing options sufficiently annoying
that I've wanted a for...if statement.

Paul
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/2YPLCIZVUIK4OIC7SNGVP3T6BEMIGJ2Y/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to