Is 'frozenset' useless in Python?

  Рет қаралды 10,058

Indently

Indently

Күн бұрын

Пікірлер: 26
@swolekhine
@swolekhine 4 ай бұрын
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 4 ай бұрын
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 4 ай бұрын
Hashing is amazing, instead of checking every single element, you can just check the hash and rest assured the equality is correct almost always
@mauriceke7581
@mauriceke7581 4 ай бұрын
I love how you explain the concepts. Straight to the point 😊
@colmx8441
@colmx8441 4 ай бұрын
"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 4 ай бұрын
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
@Anomaa
@Anomaa 4 ай бұрын
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)
@eduferreyraok
@eduferreyraok Ай бұрын
I was really hoping to see the difference in performance between these since their immutability properties should grant significant read speed improvements
@Anomaa
@Anomaa Ай бұрын
@eduferreyraok For compiled languages maybe, for Python i don't think so. Most likely set is just a subclass of frozenset with the addition of modification. Nothing more
@Elia90570
@Elia90570 4 ай бұрын
can you please talk about the memoryview type, its so confusing
@HesderOleh
@HesderOleh 4 ай бұрын
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.
@pedrokrause7553
@pedrokrause7553 4 ай бұрын
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.
@MarianoBustos-i1f
@MarianoBustos-i1f 4 ай бұрын
Probably will never use it but I will sure brag about knowing about frozensets
@murphygreen8484
@murphygreen8484 4 ай бұрын
+
@DDvargas123
@DDvargas123 4 ай бұрын
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).
@DjdDbtv
@DjdDbtv 4 ай бұрын
❤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 4 ай бұрын
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
@murphygreen8484
@murphygreen8484 4 ай бұрын
Could one use Enums to make dictionary keys?
@mdsegara101
@mdsegara101 4 ай бұрын
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 4 ай бұрын
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 4 ай бұрын
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_lofi_664
@lazy_lofi_664 4 ай бұрын
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 4 ай бұрын
Thank you 😊
@mdyousufniaz5903
@mdyousufniaz5903 4 ай бұрын
Can you make a video on cycle
@freejo4000
@freejo4000 4 ай бұрын
what i am grateful to you frozenset is hashable, can be key of dict set is not fstring with = variable declaration using :
5 Good Python Practices
23:05
Indently
Рет қаралды 22 М.
5 Classic Noob Mistakes In Python
14:39
Indently
Рет қаралды 21 М.
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
1% vs 100% #beatbox #tiktok
01:10
BeatboxJCOP
Рет қаралды 67 МЛН
Python Generators
15:32
mCoding
Рет қаралды 145 М.
10 Math Concepts for Programmers
9:32
Fireship
Рет қаралды 2 МЛН
Enums Explained In Under 12 Minutes In Python
12:03
Indently
Рет қаралды 36 М.
10 Nooby Mistakes Devs Often Make In Python
24:31
Indently
Рет қаралды 71 М.
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 67 М.
Python lists, sets, and tuples explained 🍍
15:06
Bro Code
Рет қаралды 352 М.
5 Python Libraries You Should Know in 2025!
22:30
Keith Galli
Рет қаралды 87 М.
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 262 М.
"Clean" Code, Horrible Performance
22:41
Molly Rocket
Рет қаралды 942 М.
5 Bad Ideas In Python
25:34
Indently
Рет қаралды 27 М.
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН