simplifying sorting! (all code sucks) #09

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ต.ค. 2024

ความคิดเห็น • 49

  • @petenjs3500
    @petenjs3500 10 วันที่ผ่านมา +29

    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 10 วันที่ผ่านมา +11

    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  10 วันที่ผ่านมา +5

      yep! I mentioned that via modernize (an improved 2to3 which uses six when possible)

    • @coyo_t
      @coyo_t 10 วันที่ผ่านมา +3

      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"

  • @tincustefanlucian7495
    @tincustefanlucian7495 10 วันที่ผ่านมา +4

    Simple and efficient solution. For huge dictionaries this will probably increase the performance of the app quite a lot.

  • @NiCo-gw2dh
    @NiCo-gw2dh 10 วันที่ผ่านมา +3

    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  9 วันที่ผ่านมา +2

      I have quite a few videos on that topic actually!

  • @sssaaa-v4n
    @sssaaa-v4n 9 วันที่ผ่านมา

    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!

  • @jamesarthurkimbell
    @jamesarthurkimbell 10 วันที่ผ่านมา +3

    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  10 วันที่ผ่านมา +3

      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 8 วันที่ผ่านมา +1

      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.

  • @bogaczew
    @bogaczew 10 วันที่ผ่านมา +3

    boy, it's some kindergarden optimizations

  • @madatbay
    @madatbay 3 วันที่ผ่านมา

    Would like to see more content under this theme “all code sucks”

    • @anthonywritescode
      @anthonywritescode  3 วันที่ผ่านมา

      fortunately there's a playlist in the description!

  • @avasam06
    @avasam06 8 วันที่ผ่านมา

    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)

  • @tompov227
    @tompov227 9 วันที่ผ่านมา +1

    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  9 วันที่ผ่านมา +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 9 วันที่ผ่านมา +1

      @@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

  • @sadhlife
    @sadhlife 9 วันที่ผ่านมา

    my only fear is you'll feature my code in this series someday.

  • @FunkyELF
    @FunkyELF 10 วันที่ผ่านมา +12

    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  10 วันที่ผ่านมา +7

      LLMs are definitely making this worse -- the 2 to 3 migration didn't help either (where keys / items / values returned concrete lists!)

  • @carloscorrea260
    @carloscorrea260 10 วันที่ผ่านมา

    wait me a moment, i have some refactor to do

  • @fuadnafiz98
    @fuadnafiz98 5 วันที่ผ่านมา

    Hi,
    I want to hear from you about your opinion about AI and AI tools for programming.

    • @anthonywritescode
      @anthonywritescode  5 วันที่ผ่านมา +1

      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

  • @john.darksoul
    @john.darksoul 9 วันที่ผ่านมา

    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  9 วันที่ผ่านมา

      it's not really though. sorting only can happen on a list. period. sorted takes an iterable and returns a sorted list

  • @guygeva7375
    @guygeva7375 7 วันที่ผ่านมา +1

    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  7 วันที่ผ่านมา

      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 6 วันที่ผ่านมา

      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  6 วันที่ผ่านมา

      you can just say python now. 3.8 (not a typo) is end is life

  • @matthewbell5282
    @matthewbell5282 10 วันที่ผ่านมา

    Love these bite sized videos! Super informative and quick to watch

  • @Asgallu
    @Asgallu วันที่ผ่านมา

    Pls .make more videos on this topic

    • @anthonywritescode
      @anthonywritescode  วันที่ผ่านมา

      fortunately there's a playlist with a bunch more in the description!

  • @er63438
    @er63438 9 วันที่ผ่านมา +3

    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.

  • @dcyt_
    @dcyt_ 6 วันที่ผ่านมา

    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  6 วันที่ผ่านมา

      like all examples in this series this is very real code from production (though anonymized and simplified to avoid calling out the actual authors)

  • @dera_ng
    @dera_ng 7 วันที่ผ่านมา +1

    🥹

  • @nmanduley
    @nmanduley 6 วันที่ผ่านมา

    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).

  • @mrswats
    @mrswats 10 วันที่ผ่านมา

    ACSSSSSS!

  • @jeyfus
    @jeyfus 10 วันที่ผ่านมา

    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  9 วันที่ผ่านมา +1

      idk how you would be confused though -- what else would it possibly do?

  • @drkspace
    @drkspace 10 วันที่ผ่านมา +5

    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  10 วันที่ผ่านมา +3

      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  10 วันที่ผ่านมา +5

      that said, I would not recommend C in the modern age

  • @bhaveshverma8629
    @bhaveshverma8629 8 วันที่ผ่านมา

    This is great....

  • @bacon4life
    @bacon4life 9 วันที่ผ่านมา

    I don't mind .keys(), I find it more readable.