News for hackers. Stuff that matters.
Re:Python 3.11 vs 3.10 performance by agumonkey on Wednesday July 06, 2022 @01:29PM (#32004114)
In a way it's a nice incentive not to nest too much (I have that tendency too)
Re:Python 3.11 vs 3.10 performance by yantrams on Wednesday July 06, 2022 @02:04PM (#32004604)
Re:Python 3.11 vs 3.10 performance by ChadNauseam on Wednesday July 06, 2022 @02:38PM (#32005033)
Hold mine :D https://github.com/anchpop/genomics_viz/blob/master/genomics...

That's one expression because it used to be part of a giant comprehension, but I moved it into a function for a bit more readability. I'm considering moving it back just for kicks though.

My philosophy is: if you're only barely smart enough to code it, you aren't smart enough to debug it. Therefore, you should code at your limit, to force yourself to get smarter while debugging

Re:Python 3.11 vs 3.10 performance by int_19h on Wednesday July 06, 2022 @04:10PM (#32006163)
Yours is nice and readable. Parents' one is not, but it feels like the indentation is deliberately confusing. I'd lay it out like so:

   tags = list(set([
     nel
     for subli in [
       mel
       for subl in [
         [[jel.split('/')[2:] for jel in el] 
         for el in classified
       ]
       for mel in subl 
     ]
     for nel in subli
     if nel
   ]))
Still not very readable, tho. But that's largely due to Python's outputs-first sequence comprehension syntax being a mess that doesn't scale at all.

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:

   from x in xs
   let y = x.ComputeSomething()
   where y.IsFoo && y.IsBar
   select y
In Python, you have to either invoke ComputeSomething twice, or hack "for" to work like "let" by wrapping the bound value in a container:

   y
   for x in xs
   for y in [x.ComputeSomething()]
   if y.IsFoo and y.IsBar
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.
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".
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
Re:Python 3.11 vs 3.10 performance by teddyh on Thursday July 07, 2022 @07:52AM (#32012807)
Re:Python 3.11 vs 3.10 performance by agumonkey on Wednesday July 06, 2022 @04:02PM (#32006055)
That code is fine in my book, there's more static code than for-comp and it's at work 3 level deep with no hard coupling between levels. Also well spaced and worded.
Re:Python 3.11 vs 3.10 performance by wvh on Wednesday July 06, 2022 @03:12PM (#32005438)
I don't think this is bad code. Maybe not the most Pythonic in the imperative sense, but certainly you could come across such constructs in languages that are more oriented towards functional programming. This could also be solved in a more readable manner by having a generator pipeline, though it would be a good idea to see what the performance of chained generators is like in your version and flavour of Python.
Re:Python 3.11 vs 3.10 performance by kzrdude on Wednesday July 06, 2022 @07:26PM (#32008147)
It's “not even” using for nesting in list comprehensions. This kind of thing:

   [x for stop in range(5) for x in range(stop)]