Is 'frozenset' useless in Python?

Поделиться
HTML-код
  • Опубликовано: 25 ноя 2024

Комментарии • 24

  • @swolekhine
    @swolekhine 2 месяца назад +14

    I once had to write a sequence design engine for a complicated molecular biology problem, and it involved bundling permutations of potentially hundreds of sequences (each with a unique ID) and then running computationally intensive calculations on them. I used frozensets for rapid membership checking of "have I already looked at this combination and if so, what was the result?"
    Using a (frozen)set removed the need for any kind of sorting, which was nice, considering the number of possible combinations could be enough to set your computer on fire.

    • @justsayin...1158
      @justsayin...1158 2 месяца назад

      Oh, do I understand you correctly, that you stored the sequences you already checked as frozen sets and then hashed them, to get results for sequences you have already calculated, independent of the order of the actual sequence?

    • @69k_gold
      @69k_gold 2 месяца назад

      Hashing is amazing, instead of checking every single element, you can just check the hash and rest assured the equality is correct almost always

  • @HesderOleh
    @HesderOleh 2 месяца назад +8

    I use the all the time. I use sets a lot in my programs as a lot of problems can be approached in terms of sets.
    Another thing frozensets are useful for is being able to put them into a set. You can't have a set of sets, but you can have a set of frozensets.

  • @MarianoBustos-i1f
    @MarianoBustos-i1f 2 месяца назад +7

    Probably will never use it but I will sure brag about knowing about frozensets

  • @mauriceke7581
    @mauriceke7581 2 месяца назад +3

    I love how you explain the concepts. Straight to the point 😊

    • @colmx8441
      @colmx8441 2 месяца назад

      "it states, or it shows, or there's frozenset written"
      Not really to the point!
      Also the type annotations in this video make the code much less readable and the whole concept is much more difficult to understand than it should be.
      Finally gets to the point around 3:45

  • @bloverprimal5086
    @bloverprimal5086 2 месяца назад +2

    It also can be used to simulate mathematical definition of ordered pairs, which is defined as set of sets. Being hashable, it can be in set or frozenset so can simulate mathematical concepts

  • @DDvargas123
    @DDvargas123 2 месяца назад +12

    I'm mostly a hobbyist programmer so whenever I feel the need for a "hashable set" I just call `tuple(sorted(the_set))` (works well at least for smallish data).

  • @Anomaa
    @Anomaa 2 месяца назад +1

    whenever I want a constant that is a collection, I almost always use a frozenset. The reasons:
    - immutable: secure and small memory size (like a tuple)
    - `in` operation is much faster than a tuple when the collection size becomes large enough (O(1) like a set)

  • @pedrokrause7553
    @pedrokrause7553 2 месяца назад

    Finally someone mentioning frozenset and it's uses. Unfortunately, the projects where it would have been useful I didn't know about it at the time.

  • @MMarcuzzo
    @MMarcuzzo 2 месяца назад +2

    Also, I think you can make sets of sets using frozenset. You cant with just set.
    There many math scenarios that involves set of sets, of sets...
    So it can be useful.
    Also, immutables are good practice. Check out some functional programming principles

  • @DjdDbtv
    @DjdDbtv 2 месяца назад +6

    ❤I hope you can explain Python libraries such as ms4, bs4, re, mechanicalalsoup, socket, and more libraries such as platform, and more and more, please.🌹❤😢

  • @Elia90570
    @Elia90570 2 месяца назад +4

    can you please talk about the memoryview type, its so confusing

  • @mdsegara101
    @mdsegara101 2 месяца назад

    up till now, i'm using it for a key of a dictionary when doing a value mapping, when the initial value is a set

  • @TheJamesM
    @TheJamesM 2 месяца назад

    I first encountered frozensets when I was researching whether it was possible to do something similar to the permissions example. My main problem with it is superficial - it's quite a lot of characters every single time you want to create one, which can make for some awkward-looking code, particularly if you're trying to follow line length standards.
    While watching this is occurred to me that you could make a class with convenience __getitem__ and __setitem__ methods to handle converting keys to frozensets if necessary. Probably not the best idea in a performance-sensitive context, but might be handy elsewhere.

    • @TheJamesM
      @TheJamesM 2 месяца назад

      Maybe something like this (though I'm sure it can be massively improved):
      ```
      class SetKeyedDict(dict):
      @staticmethod
      def sanitize_key(key):
      if isinstance(key, str):
      key = frozenset((key,))
      elif not isinstance(key, frozenset):
      key = frozenset(key)
      return key

      def __init__(self, *args, **kwargs):
      # TODO: Sanitize initial input
      super().__init__(*args, **kwargs)
      def __getitem__(self, key):
      return super().__getitem__(self.sanitize_key(key))
      def __setitem__(self, key, value):
      super().__setitem__(self.sanitize_key(key), value)
      def __delitem__(self, key):
      super().__delitem__(self.sanitize_key(key))
      def __contains__(self, key):
      return super().__contains__(self.sanitize_key(key))
      ```
      I leave type hinting as an exercise for the reader 😉
      (I gave it a go, but quickly realized it would get quite confusing given its generic nature. I'm sure the recent Carberra video on Generics would have helped, but I didn't want to get completely sidetracked.)
      It would be really handy if there were a way to preprocess all key references without having to overload every method, but from some brief searching I couldn't find a way.

  • @lazy_lofi223
    @lazy_lofi223 2 месяца назад

    can you make a tutorial on using async function in a non async function.
    like
    calling a async function in a non async function using loops, like loops=asyncio.get_running_loop()
    i seriously don't know how to say that, 😅
    I don't wanna use asyncio.run() on every function in a class

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 2 месяца назад

    Thank you 😊

  • @murphygreen8484
    @murphygreen8484 2 месяца назад

    Could one use Enums to make dictionary keys?

  • @mdyousufniaz5903
    @mdyousufniaz5903 2 месяца назад +1

    Can you make a video on cycle

  • @freejo4000
    @freejo4000 2 месяца назад

    what i am grateful to you
    frozenset is hashable, can be key of dict
    set is not
    fstring with =
    variable declaration using :