Hello,
It is likely not the first time such a proposal is being made but let's
see.
I would like to explicitly set variable names in list comprehensions using
a where keyword, eventually set after the if keyword:
[price for item in basket if price is not None where price := item.get(
'price')]
For this example one could use the walrus operator which is indeed smaller:
[price for item in basket if price := item.get('price') is not None]
But I fell that this approach is a bit opportunistic as one is doing two
things at a time, if you allow me more lines for a somewhat more complex
example:
[
price * (1 + vat)
for
item in basket
if
price is not None
where
price := item.get('price')
vat := get_vat(item, user)
]
Now this example may look pretty stupid and probably one may simply use for
loop, but I feel that this kind of Haskell-like where inside of list
comprehension will let the programmer have a space where they can
explicitly state the variables.
Best regards,
Fabrizio
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/AHI6NIMZNFVL2ROW6CD2HFBAPL7KCSW7/
Code of Conduct: http://python.org/psf/codeofconduct/