simplifying sorting! (all code sucks) #09

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

anthonywritescode

anthonywritescode

Күн бұрын

Пікірлер: 50
@petenjs3500
@petenjs3500 Ай бұрын
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 Ай бұрын
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 Ай бұрын
yep! I mentioned that via modernize (an improved 2to3 which uses six when possible)
@coyo_t
@coyo_t Ай бұрын
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 Ай бұрын
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 Ай бұрын
I have quite a few videos on that topic actually!
@jamesarthurkimbell
@jamesarthurkimbell Ай бұрын
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 Ай бұрын
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 Ай бұрын
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.
@sssaaa-v4n
@sssaaa-v4n Ай бұрын
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 Ай бұрын
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 Ай бұрын
boy, it's some kindergarden optimizations
@tompov227
@tompov227 Ай бұрын
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 Ай бұрын
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 Ай бұрын
@@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
@madatbay
@madatbay Ай бұрын
Would like to see more content under this theme “all code sucks”
@anthonywritescode
@anthonywritescode Ай бұрын
fortunately there's a playlist in the description!
@madatbay
@madatbay Ай бұрын
Unfortunately or fortunately I’ve consumed the whole playlist + some of your other videos, looking forward for new “all code sucks” content
@FunkyELF
@FunkyELF Ай бұрын
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 Ай бұрын
LLMs are definitely making this worse -- the 2 to 3 migration didn't help either (where keys / items / values returned concrete lists!)
@guygeva7375
@guygeva7375 Ай бұрын
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 Ай бұрын
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 Ай бұрын
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 Ай бұрын
you can just say python now. 3.8 (not a typo) is end is life
@fuadnafiz98
@fuadnafiz98 Ай бұрын
Hi, I want to hear from you about your opinion about AI and AI tools for programming.
@anthonywritescode
@anthonywritescode Ай бұрын
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
@sadhlife
@sadhlife Ай бұрын
my only fear is you'll feature my code in this series someday.
@carloscorrea260
@carloscorrea260 Ай бұрын
wait me a moment, i have some refactor to do
@john.darksoul
@john.darksoul Ай бұрын
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 Ай бұрын
it's not really though. sorting only can happen on a list. period. sorted takes an iterable and returns a sorted list
@dera_ng
@dera_ng Ай бұрын
🥹
@tincustefanlucian7495
@tincustefanlucian7495 Ай бұрын
Simple and efficient solution. For huge dictionaries this will probably increase the performance of the app quite a lot.
@drkspace
@drkspace Ай бұрын
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 Ай бұрын
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 Ай бұрын
that said, I would not recommend C in the modern age
@er63438
@er63438 Ай бұрын
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.
@matthewbell5282
@matthewbell5282 Ай бұрын
Love these bite sized videos! Super informative and quick to watch
@mrswats
@mrswats Ай бұрын
ACSSSSSS!
@dcyt_
@dcyt_ Ай бұрын
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 Ай бұрын
like all examples in this series this is very real code from production (though anonymized and simplified to avoid calling out the actual authors)
@Asgallu
@Asgallu Ай бұрын
Pls .make more videos on this topic
@anthonywritescode
@anthonywritescode Ай бұрын
fortunately there's a playlist with a bunch more in the description!
@nmanduley
@nmanduley Ай бұрын
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 Ай бұрын
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 Ай бұрын
idk how you would be confused though -- what else would it possibly do?
@bacon4life
@bacon4life Ай бұрын
I don't mind .keys(), I find it more readable.
@bhaveshverma8629
@bhaveshverma8629 Ай бұрын
This is great....
Sequence[str] is a tiny landmine (all code sucks) #10
7:05
anthonywritescode
Рет қаралды 6 М.
adding test == others fail??? (intermediate) anthony explains #572
10:40
anthonywritescode
Рет қаралды 4,3 М.
The Singing Challenge #joker #Harriet Quinn
00:35
佐助与鸣人
Рет қаралды 46 МЛН
Thank you Santa
00:13
Nadir Show
Рет қаралды 27 МЛН
ТЫ В ДЕТСТВЕ КОГДА ВЫПАЛ ЗУБ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 4,6 МЛН
This Game Is Wild...
00:19
MrBeast
Рет қаралды 169 МЛН
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 852 М.
crossing the streams (all code sucks) #12
6:17
anthonywritescode
Рет қаралды 4 М.
oops I'm the pyuwsgi maintainer now (intermediate) anthony explains #579
22:55
5 Wacky Python Features
15:37
Indently
Рет қаралды 22 М.
Programming Is Cooked
9:30
ThePrimeTime
Рет қаралды 160 М.
GitHub's Spam Problem Keeps Getting Worse....
18:38
Theo - t3․gg
Рет қаралды 70 М.
C++ vs Rust: which is faster?
21:15
fasterthanlime
Рет қаралды 404 М.
The Singing Challenge #joker #Harriet Quinn
00:35
佐助与鸣人
Рет қаралды 46 МЛН