[Python-ideas] A real life example of "given"
Neil Girdhar
mistersheik at gmail.com
Wed May 30 13:59:37 EDT 2018
On Wed, May 30, 2018 at 1:56 PM Neil Girdhar <mistersheik at gmail.com> wrote:
> On Wed, May 30, 2018 at 1:52 PM Chris Angelico <rosuav at gmail.com> wrote:
>
>> On Thu, May 31, 2018 at 1:23 AM, Peter O'Connor
>> <peter.ed.oconnor at 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 ":="
>>
>> I don't understand what you're showcasing here. With :=, you give a
>> name to something at the exact point that it happens:
>>
>> updates = {
>> y: do_something_to(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
>> }
>>
>> Personally, I'd use a shorter name for something that's used in such a
>> small scope (same as you use one-letter "x" and "y"). But that's the
>> only way that the 'given' syntax looks at all better - by encouraging
>> you to use yet another line, it conceals some of its immense
>> verbosity. (Note how the name "potential_update" is used twice with
>> :=, once to set and one to retrieve; but with given, it's used three
>> times - retrieve, retrieve, and set.)
>>
>> How does this show that 'given' is more flexible?
>>
>>
> Oh yeah, good point, I forgot that I could use := within the condition
> itself. It does show that this feature is useful, but not that given is
> more flexible than :=.
>
This example shows additional flexibility:
z = {a: transformed_b
for b in bs
given transformed_b = transform(b)
for a in as_}
There is no nice, equivalent := version as far as I can tell.
>
>
>> ChrisA
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at 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 at googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180530/9c0f1602/attachment-0001.html>
More information about the Python-ideas
mailing list