News for hackers. Stuff that matters.
Re:Python 3.11 vs 3.10 performance by texaslonghorn5 on Wednesday July 06, 2022 @04:15PM (#32006225)
Does the new walrus := in python solve your problem?
Re:Python 3.11 vs 3.10 performance by roelschroeven on Wednesday July 06, 2022 @06:29PM (#32007685)
The official name is assignment expression, which can be good to know to find documentation. Here's the PEP: https://peps.python.org/pep-0572/
Re:Python 3.11 vs 3.10 performance by otherme123 on Thursday July 07, 2022 @01:57AM (#32010407)
Curiously, searching the docs for "walrus" (https://docs.python.org/3/search.html?q=walrus) yields exactly the results you need, but searching for "assignment expression" yields the previous results mixed a lot of other results due to the word "expression".
Re:Python 3.11 vs 3.10 performance by int_19h on Wednesday July 06, 2022 @09:25PM (#32008944)
Not really; where would you put it inside the []?
Re:Python 3.11 vs 3.10 performance by dragonwriter on Wednesday July 06, 2022 @09:33PM (#32008981)
In the example from the end of the earlier post:

   y
   for x in xs
   if (y:=x.ComputeSomething()).IsFoo and y.IsBar
Re:Python 3.11 vs 3.10 performance by int_19h on Wednesday July 06, 2022 @11:20PM (#32009574)
I suppose that works, although readability is not great, to put it mildly.
Re:Python 3.11 vs 3.10 performance by texaslonghorn5 on Wednesday July 06, 2022 @11:21PM (#32009580)
Much better would be

[(y:=x.ComputeSomething()) for x in xs if y.IsFoo and y.IsBar]

Re:Python 3.11 vs 3.10 performance by int_19h on Thursday July 07, 2022 @03:01AM (#32010755)
That doesn't work, because that first subexpression is evaluated last, after "if".
Re:Python 3.11 vs 3.10 performance by ChadNauseam on Wednesday July 06, 2022 @11:37PM (#32009649)
For another example of how to use the walrus operator, I use it multiple times in the code I linked a few comments up