[Python-ideas] A real life example of "given"
Steven D'Aprano
steve at pearwood.info
Wed May 30 08:22:19 EDT 2018
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
More information about the Python-ideas
mailing list