Is 'frozenset' useless in Python?

Поделиться
HTML-код
  • Опубликовано: 24 сен 2024
  • When you start learning Python, chances are that you're not really ever going to learn about frozen sets in detail. So let's cover the question: is frozenset useless in Python?
    Follow my WhatsApp channel:
    whatsapp.com/c...
    Learn more about frozenset performance:
    stackoverflow....
    ▶ Become job-ready with Python:
    www.indently.io
    ▶ Follow me on Instagram:
    / indentlyreels

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

  • @swolekhine
    @swolekhine 20 дней назад +12

    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 19 дней назад

      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 18 дней назад

      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 20 дней назад +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 20 дней назад +6

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

  • @Anomaa
    @Anomaa 17 дней назад +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)

  • @bloverprimal5086
    @bloverprimal5086 20 дней назад +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 20 дней назад +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).

  • @mauriceke7581
    @mauriceke7581 20 дней назад +3

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

    • @colmx8441
      @colmx8441 20 дней назад

      "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

  • @Elia90570
    @Elia90570 20 дней назад +4

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

  • @DjdDbtv
    @DjdDbtv 20 дней назад +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.🌹❤😢

  • @MMarcuzzo
    @MMarcuzzo 19 дней назад +1

    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

  • @pedrokrause7553
    @pedrokrause7553 19 дней назад

    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.

  • @mdsegara101
    @mdsegara101 20 дней назад

    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 19 дней назад

    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 19 дней назад

      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 19 дней назад

    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

  • @murphygreen8484
    @murphygreen8484 20 дней назад

    Could one use Enums to make dictionary keys?

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 20 дней назад

    Thank you 😊

  • @mdyousufniaz5903
    @mdyousufniaz5903 20 дней назад +1

    Can you make a video on cycle

  • @freejo4000
    @freejo4000 20 дней назад

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