simplifying sorting! (all code sucks) #09

  Рет қаралды 7,194

anthonywritescode

anthonywritescode

Күн бұрын

Пікірлер
@petenjs3500
@petenjs3500 2 ай бұрын
It's such a short and simple video, yet listening to a professional use the correct terminology to describe these simple things and dropping some nice more complex facts along the way, is a really great way to learn. Thank you Anthony!
@tylermfdurden
@tylermfdurden 2 ай бұрын
I had to deal with a lot of this when using 2to3 on a fairly large code base. The conversion tool doesn't know whether the return from a .keys() call is going to be stored in a variable or immediately iterated over; it just knows that in Python 2, calling keys would give you a list so it preserves that for your new Python 3 code by wrapping any call to keys in a call to list. Same for a whole ton of stuff in Python 3 that does lazy iteration using views or range objects... It's a bit silly because the whole reason Python 3 switched to this behavior is for speed and performance when iterating or chaining calls to zip, map, filter, etc. And by wrapping everything in list calls you lose that performance gain.
@anthonywritescode
@anthonywritescode 2 ай бұрын
yep! I mentioned that via modernize (an improved 2to3 which uses six when possible)
@coyo_t
@coyo_t 2 ай бұрын
i converted a fairly large python 2 program to python 3 by hand and oh boy the old behavior of returning lists vs the new behavior of iterators was annoying to debug. I understand why theyre doing that there since this thing was relying on the returned values explicitly being lists in a lot of places i just wound up wrapping a bunch of things in list() to preserve behavior any time i tried to fix the program "Properly" it just wouldnt work because it was an ugly spiderweb that was hard to keep track of so i just did the lazy thing :/ "It Works I Guess"
@NiCo-gw2dh
@NiCo-gw2dh 2 ай бұрын
The optimized code is so much easier to read! Now I also have an explanation why I should not use .keys() even though people told me it makes the code easier... Would be great to see more advanced (performance) optimizations, maybe even using a profiler.
@anthonywritescode
@anthonywritescode 2 ай бұрын
I have quite a few videos on that topic actually!
@jamesarthurkimbell
@jamesarthurkimbell 2 ай бұрын
I used to find it hard to remember that sorted and reversed, despite their parallel names, don't return the same type. Maybe that's why we see people add redundant list conversions.
@anthonywritescode
@anthonywritescode 2 ай бұрын
the way I think about it is that in order to sort you need some concrete structure whereas reversing many datastructures can be done straight from iterating!
@avasam06
@avasam06 2 ай бұрын
When you don't have any sort of editor hint and are left guessing, I can see people just constantly coercing stuff into lists just to be safe.
@tompov227
@tompov227 2 ай бұрын
I don't know why I thought sorted() returned a generator like everything else in python but it's good to know that it does not. I am definitely guilty of "definitive list()-ing" when I don't wanna check and know I need to use indexing
@anthonywritescode
@anthonywritescode 2 ай бұрын
if you think about it there's not really any way you can return a sort iteratively. you need the whole structure to manipulate in order to sort
@tompov227
@tompov227 2 ай бұрын
@@anthonywritescode that makes sense which is why it seems obvious now lol I just always think in my head pythons gonna return a generator but yeah definitely need access to all elements to sort
@sssaaa-v4n
@sssaaa-v4n 2 ай бұрын
I've actually done the dct.keys() more times that I want to admit. I always forget that it iterates over the keys by default. Anyway great video Anthony!
@avasam06
@avasam06 2 ай бұрын
I just made a PR de-2to3fying exactly this a few weeks back in pywin32 ! Almost done passing pyupgrade on it too (only got some f-string improvements left)
@bogaczew
@bogaczew 2 ай бұрын
boy, it's some kindergarden optimizations
@FunkyELF
@FunkyELF 2 ай бұрын
The amount of no-ops I find in Python code is substantial. Makes it less readable. I think LLMs are making this even worse unfortunately.
@anthonywritescode
@anthonywritescode 2 ай бұрын
LLMs are definitely making this worse -- the 2 to 3 migration didn't help either (where keys / items / values returned concrete lists!)
@fuadnafiz98
@fuadnafiz98 2 ай бұрын
Hi, I want to hear from you about your opinion about AI and AI tools for programming.
@anthonywritescode
@anthonywritescode 2 ай бұрын
they are maybe useful for extremely simple tasks but often produce completely meaningless garbage and ime waste massive amounts of time and energy for little benefit
@madatbay
@madatbay 2 ай бұрын
Would like to see more content under this theme “all code sucks”
@anthonywritescode
@anthonywritescode 2 ай бұрын
fortunately there's a playlist in the description!
@madatbay
@madatbay 2 ай бұрын
Unfortunately or fortunately I’ve consumed the whole playlist + some of your other videos, looking forward for new “all code sucks” content
@dcyt_
@dcyt_ 2 ай бұрын
Did you make this code yourself for educational purposes? I just cannot imagine a real human being writing anything like that for production :)
@anthonywritescode
@anthonywritescode 2 ай бұрын
like all examples in this series this is very real code from production (though anonymized and simplified to avoid calling out the actual authors)
@john.darksoul
@john.darksoul 2 ай бұрын
It's so counterintuitive that calling sorted on a dict not only returns an object of a different type (I could understand if it returned something like a sorteddict if that's a thing), but that type isn't even a mapping Weird stuff
@anthonywritescode
@anthonywritescode 2 ай бұрын
it's not really though. sorting only can happen on a list. period. sorted takes an iterable and returns a sorted list
@guygeva7375
@guygeva7375 2 ай бұрын
In my opinion calling keys here makes it simpler, since `sorted(dct)` reads as if you are sorting the dict itself, while `sorted(dct.keys())` is quicker to read and comprehend
@anthonywritescode
@anthonywritescode 2 ай бұрын
you could make the same argument for `list(sorted(dct))` now I don't need to know if it's a list -- but it's wasteful (and ignorant) :)
@cben
@cben 2 ай бұрын
To make it extra confusing, Python 3.6+ dictionaries have their own concept of order (they remember insertion order). Personally, I'd write sorted(dct) in an experienced pythonists team vs. sorted(dct.keys()) in a polyglot team for clarity.
@anthonywritescode
@anthonywritescode 2 ай бұрын
you can just say python now. 3.8 (not a typo) is end is life
@carloscorrea260
@carloscorrea260 2 ай бұрын
wait me a moment, i have some refactor to do
@sadhlife
@sadhlife 2 ай бұрын
my only fear is you'll feature my code in this series someday.
@dera_ng
@dera_ng 2 ай бұрын
🥹
@tincustefanlucian7495
@tincustefanlucian7495 2 ай бұрын
Simple and efficient solution. For huge dictionaries this will probably increase the performance of the app quite a lot.
@Asgallu
@Asgallu 2 ай бұрын
Pls .make more videos on this topic
@anthonywritescode
@anthonywritescode 2 ай бұрын
fortunately there's a playlist with a bunch more in the description!
@er63438
@er63438 2 ай бұрын
The default of iterating a dictionary being the keys IMHO is not intuitive. As you mentioned, other languages behave differently. I always prefer doing calling `.keys()` in favor of being explicit.
@drkspace
@drkspace 2 ай бұрын
I think calling dct.keys() is better as is more explicit and you don't have to remember what dct iterates. If you're that worried about performance, you're better off writing all or some of the code in c/c++/rust.
@anthonywritescode
@anthonywritescode 2 ай бұрын
you're absolutely better to write performance critical code in not-python, but it's just as easy to use the not-wasteful syntax
@anthonywritescode
@anthonywritescode 2 ай бұрын
that said, I would not recommend C in the modern age
@mrswats
@mrswats 2 ай бұрын
ACSSSSSS!
@matthewbell5282
@matthewbell5282 2 ай бұрын
Love these bite sized videos! Super informative and quick to watch
@nmanduley
@nmanduley 2 ай бұрын
The code at the end is certainly the most optimized code possible for that task. However, I would probably still recommend to leave the dct.keys() just to make it more explicit. Sometimes a little redundancy does wonders for the readability of the code (unless the task demands for maximum optimization, in that case by all means remove it).
@jeyfus
@jeyfus 2 ай бұрын
Thank you for yet another great vid :) Though I really liked the way the example snippet was optimized, one who isn’t familiar enough with the sorted() func could easily be confused by the sorted(dict_instance) syntax. It’s easy to assume that it sorts the dictionary
@anthonywritescode
@anthonywritescode 2 ай бұрын
idk how you would be confused though -- what else would it possibly do?
@bacon4life
@bacon4life 2 ай бұрын
I don't mind .keys(), I find it more readable.
@bhaveshverma8629
@bhaveshverma8629 2 ай бұрын
This is great....
Sequence[str] is a tiny landmine (all code sucks) #10
7:05
anthonywritescode
Рет қаралды 6 М.
КОНЦЕРТЫ:  2 сезон | 1 выпуск | Камызяки
46:36
ТНТ Смотри еще!
Рет қаралды 3,7 МЛН
She wanted to set me up #shorts by Tsuriki Show
0:56
Tsuriki Show
Рет қаралды 8 МЛН
Pydantic is OP, here's why
18:10
Carberra
Рет қаралды 24 М.
Mojo v. Python v. Julia: Advent of Code 2024, Day 04
18:23
My FAVORITE Error Handling Technique
16:01
ArjanCodes
Рет қаралды 45 М.
Why You Should Think Twice Before Using Returns in Python
21:27
ArjanCodes
Рет қаралды 50 М.
crossing the streams (all code sucks) #12
6:17
anthonywritescode
Рет қаралды 4,6 М.
5 Good Python Habits
17:35
Indently
Рет қаралды 678 М.
I fixed my computer!
21:09
anthonywritescode
Рет қаралды 7 М.
КОНЦЕРТЫ:  2 сезон | 1 выпуск | Камызяки
46:36
ТНТ Смотри еще!
Рет қаралды 3,7 МЛН