[Python-Dev] The `for y in [x]` idiom in comprehensions

Barry Warsaw barry at python.org
Thu Feb 22 16:33:35 EST 2018


On Feb 22, 2018, at 11:04, Serhiy Storchaka <storchaka at gmail.com> wrote:
> 
> Stephan Houben proposed an idiom which looks similar to new hypothetic syntax:
> 
>    result = [y + g(y) for x in range(10) for y in [f(x)]]
> 
> `for y in [expr]` in a comprehension means just assigning expr to y. I never seen this idiom before, but it can be a good replacement for a hypothetic syntax for assignment in comprehensions. It changes the original comprehension less than other approaches, just adds yet one element in a sequence of for-s and if-s. I think that after using it more widely it will become pretty idiomatic.

My questions are 1) will this become idiomatic enough to be able to understand at a glance what is going on, rather than having to pause to reason about what that 1-element list-like syntax actually means, and 2) will this encourage even more complicated comprehensions that are less readable than just expanding the code into a for-loop?

for-loops-are-not-evil-ly y’rs,
-Barry

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Message signed with OpenPGP
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180222/2220dfb0/attachment.sig>


More information about the Python-Dev mailing list