[Python-ideas] A real life example of "given"

Neil Girdhar mistersheik at gmail.com
Wed May 30 14:34:01 EDT 2018

Peter wrote:

> Well you could just do:

    z = {a: b
         for b in (transform(bi) for bi in bs)
         for a in as_}

That works, but I prefer the implicit nesting of a sequence of "comp_for"
expressions to a the nested generator.

On Wed, May 30, 2018 at 2:16 PM Chris Angelico <rosuav at gmail.com> wrote:

> On Thu, May 31, 2018 at 3:59 AM, Neil Girdhar <mistersheik at gmail.com>
> wrote:
> > This example shows additional flexibility:
> >
> > z = {a: transformed_b
> >      for b in bs
> >      given transformed_b = transform(b)
> >      for a in as_}
> >
> > There is no nice, equivalent := version as far as I can tell.
> True. However, it took me several readings to understand what you were
> doing here. I think I actually prefer "for transformed_b in
> [transform(b)]" to this syntax, which is saying something.

I feel you.  I think of "given" as an assignment that is in front of the
expression, just like "for" (in comp_for) is a for loop that is in front,
and "if" (in comp_if) is a condition that is in front.

> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
> --
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/keaR3FudcwQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180530/375ef3c3/attachment.html>

More information about the Python-ideas mailing list