[Python-ideas] Assignments in list/generator expressions

Eugene Toder eltoder at gmail.com
Tue Apr 12 14:27:03 CEST 2011


On Tue, Apr 12, 2011 at 1:47 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> You know, I may have spoken too soon in saying PEP 3150 couldn't help
> with the list comprehension use case.

Alternatively, comprehension can be used instead of PEP 3150, e.g.

desired_property = next(
    calc_value(temp, depth, purity, salinity, size, density)
    for sea in [water()]
    for temp in [get_temperature(sea)]
    for depth in [get_depth(sea)]
    for purity in [get_purity(sea)]
    for saltiness in [get_salinity(sea)]
    for size in [get_size(sea)]
    for density in [get_density(sea)]
)
# Further operations using desired_property

> That "given" after the expression keeps throwing me off.  It just looks out
> of place.  Would something like the following be feasible:
> c = sqrt(a*a + b*b) given:
>     a = retrieve_a()
>     b = retrieve_b()

In Haskell it's usually indented differently:

c = sqrt(a*a + b*b)
      given:
          a = retrieve_a()
          b = retrieve_b()

Eugene



More information about the Python-ideas mailing list