[Python-ideas] A real life example of "given"
Neil Girdhar
mistersheik at gmail.com
Wed May 30 05:42:21 EDT 2018
I thought I would share a recent use I had for "given":
I have this comprehension:
potential_updates = {y: command.create_potential_update(y)
for x in need_initialization_nodes
for y in [x, *x.synthetic_inputs()]}
I want to filter out values that are None. I don't want to call the
function call twice, so I have to resort to using a loop and appending or
the for z in [y] trick. With "given", I can write:
potential_updates = {
y: potential_update
for x in need_initialization_nodes
for y in [x, *x.synthetic_inputs()]
given potential_update = command.create_potential_update(y)
if potential_update is not None}
I also wanted to point out that code like this is self-commenting because
the key and value of the comprehension can be given names.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180530/bfb152d6/attachment.html>
More information about the Python-ideas
mailing list