5 ms·
When is that coming? Yes the error messages are confusing in a big list comprehension.
by bilsbie 4y ago
When is that coming? Yes the error messages are confusing in a big list comprehension.
- alexfromapex 4y agoWill be in the 3.11 release: https://docs.python.org/3.11/whatsnew/3.11.html#new-features https://docs.python.org/3.11/whatsnew/3.11.html#new-features
- teekert 4y agoWhenever I nest a list comprehension I feel dirty anyway, I wonder if a better error message will help solve something that is just hard to grasp sometimes.
- 8organicbits 4y ago"Debugging is twice as hard as writing the code in the first place. Therfore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Rajanand Although, personally, I enjoy python list comprehensions.
- timoteostewart 4y agoI believe that quote about debugging being twice as hard as coding is by Brian Kernighan. https://en.m.wikiquote.org/wiki/Brian_Kernighan https://en.m.wikiquote.org/wiki/Brian_Kernighan
- 8organicbits 4y agoMaybe, I grabbed the first matching quote I found. I can't attribute it on my own.
- timoteostewart 4y agoNo sweat. I’m a quote nerd so it jumped out at me
- polski-g 4y agoAll list comprehensions should error with message "FATAL PARSE ERROR: Will you really understand this in six months?"
- TremendousJudge 4y agoIf people complain about Rust's borrow checker already...
- wheelerof4te 4y agoList comprehensions should be used for making quick lists that contain simple calculations or filters. Anything more will come back to bite you later.
- teekert 4y agoSomething like this I find ok (just a filter): [x for x in y if x is not z] Sometimes though, facilitated by Jupyter's notebooks making executing as you build the code very easy, I create a beast like this: [os.path.join([x for x in y if x is not z]) + '/bin' for a in b if 'temp' not in a] Yes this is fictional and probably contains an error but you get the point, you can filter lists of lists like this, but it's really unfriendly to anyone trying to understand it later.
- mikepurvis 4y agoNot even with nesting— as soon as it's long enough that I'm breaking it into multiple lines, I'm immediately like, okay this is long enough to just be a normal for-loop, or maybe a generator function. But it doesn't need to be a list comprehension any more.
- agumonkey 4y agoIn a way it's a nice incentive not to nest too much (I have that tendency too)
- yantrams 4y agoHold my beer! https://i.redd.it/8waggyjyyle51.png https://i.redd.it/8waggyjyyle51.png
- ChadNauseam 4y agoHold mine :D https://github.com/anchpop/genomics_viz/blob/master/genomics-viz/Assets/StreamingAssets/Chromosomal/Scripts/chromsdata_tools/chromsdata_tools/compilation.py#L292-L433 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
- teddyh 4y agohttps://www.reddit.com/r/ProgrammerHumor/comments/uosex4/no_please_no/ https://www.reddit.com/r/ProgrammerHumor/comments/uosex4/no_...
- agumonkey 4y agoThat 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.
- int_19h 4y agoYours 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.
- psalminen 4y agoBig list comprehensions cause confusion in many ways. KISS is crucial with them.
- mcronce 4y agoI agree. The rule of thumb I follow is that if the list comp doesn't fit on one line, whatever I'm doing is probably too complicated for a list comp.
- gpderetta 4y agoBut why? The only reason is that error messages are awful and intermediate values can't be named. There is in principle no reason why list comprehension need to be worse than straight line for loops. The deciding factor to chose betwee the two should be whether I'm writing something for side effects as opposed to for its value.
- mcronce 4y agoYou just listed two very good reasons: Intermediate values can't be named, and error messages are awful.