When using for x in obj notation, does iterating over a frozenset use less memory than a set? Or when is there a memory advantage to using a frozen set opposed to a set?
I doubt there is going to be a significant (if any) memory savings from using frozenset over set. The reason to use a frozenset is that it is hashable, so you can use it as a dictionary key (or as an element of another set).
Looking at the python2.7 source, the empty frozen set is a singleton and making a frozenset from an existing frozenset simply returns the old reference. Otherwise, there doesn't seem to be any significant memory advantage as both frozenset_new and set_new typically end up calling make_new_set with the same arguments.
The reason to use a frozenset rather than a regular set is to reap the benefits of immutability -- e.g. to use an instance because it is hashible or because it is meant to be a constant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With