Re:Python 3.11 vs 3.10 performance
by dragonwriter on Wednesday July 06, 2022 @10:20PM
(#32009272)
> But that's largely due to Python's outputs-first sequence comprehension syntax being a mess that doesn't scale at all.
Choices like wrapping a set constructor around a list comprehension rather than just using a set comprehension don't help, neither does using multiple nested no-transformation comprehensions laid out that way.
By hand, on mobile, I think this reduces to (assuming “from itertools import chain” in the file header):
tags = list({
nel
for subli in chain.from_iterables(chain(
[[jel.split('/')[2:] for jel in el]
for el in classified))
for nel in subli
if nel
})
Re:Python 3.11 vs 3.10 performance
by andrewaylett on Thursday July 07, 2022 @10:56AM
(#32014904)
Since it's all generator expressions internally anyway, an intermediate doesn't actually add appreciable (any?) runtime overhead but does make the code more readable:
ys = (x.ComputeSomething() for x in xs)
result = [y for y in ys if y.isFoo and y.isBar]
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".
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.IsBarI 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]
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
Side note: one other thing I always hated about those things in Python is that there's no way to bind an intermediary computation to a variable. In C# LINQ, you can do things like:
In Python, you have to either invoke ComputeSomething twice, or hack "for" to work like "let" by wrapping the bound value in a container: It's not just about not repeating yourself or not running the same code twice, either - named variables are themselves a form of self-documenting code, and a sequence pipeline using them even where they aren't strictly needed can be much more readable.