On Wed, May 30, 2018 at 11:32 AM Peter O'Connor <peter.ed.oconnor@gmail.com> wrote:
 In comparison, I think that := is much simpler. 

In this case that's true, but a small modification:

        updates = {
            y: do_something_to(potential_update)
            for x in need_initialization_nodes
            for y in [x, *x.synthetic_inputs()]
            if potential_update is not None
            given potential_update = command.create_potential_update(y)
            }

Shows the flexibility of this given syntax vs ":="

If we think of "given" as just inserting a line with variable-definitions before the preceding statement, it seems clear that:

        updates = {
            y: potential_update
            given potential_update = command.create_potential_update(y)
            for x in need_initialization_nodes
            for y in [x, *x.synthetic_inputs()]
            if potential_update is not None
            }


Should raise a NameError: name 'potential_update' is not defined, and

        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
            }



Should raise a NameError: name 'y' is not defined.

The reason I want it like that for comprehensions is that I think of it as equivalent to:

updates = {}
for x in need_initialization_nodes:
    for y in [x, *x.synthetic_inputs()]:
        potential_update = command.create_potential_update(y)
        if potential_update is not None:
            updates[y] = potential_update

But you're right that this would be a second addition to the grammar.  One addition would be to "test" for something like

test: bool_test [comp_given]
bool_test: or_test ['if' or_test 'else' test] | lambdef
comp_given: 'given' testlist_star_expr annassign

The second would permit the usage in comprehensions:

comp_iter: comp_for | comp_if | comp_given

Best,

Neil

For safety it seems reasonable that if a variable is "given" in a comprehension, trying to refer to it (even if it defined in the enclosing scope) before the inner-definition will result in a NameError.


On Wed, May 30, 2018 at 2:22 PM, Steven D'Aprano <steve@pearwood.info> wrote:
On Wed, May 30, 2018 at 02:42:21AM -0700, Neil Girdhar wrote:

> 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'm not sure if that would be legal for the "given" syntax. As I
understand it, the "given" syntax is:

    expression given name = another_expression

but you've got half of the comprehension stuffed in the gap between the
leading expression and the "given" keyword:

    expression COMPREH- given name = another_expression -ENSION

so I think that's going to be illegal.


I think it wants to be written this way:

        potential_updates = {
            y: potential_update
            for x in need_initialization_nodes
            for y in [x, *x.synthetic_inputs()]
            if potential_update is not None
            given potential_update = command.create_potential_update(y)
            }


Or maybe it should be this?

        potential_updates = {
            y: potential_update
            given potential_update = command.create_potential_update(y)
            for x in need_initialization_nodes
            for y in [x, *x.synthetic_inputs()]
            if potential_update is not None
            }


I'm damned if I know which way is correct. Either of them? Neither?

In comparison, I think that := is much simpler. There's only one place
it can go:

        potential_updates = {
            y: potential_update
            for x in need_initialization_nodes
            for y in [x, *x.synthetic_inputs()]
            if (
                potential_update := command.create_potential_update(y)
               ) is not None
            }


--
Steve
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

--

---
You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/keaR3FudcwQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-ideas+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

--

---
You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/keaR3FudcwQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-ideas+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.