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.
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to