I find it odd that in Python, {1} == frozenset({1})
evaluates to True
. set
and frozenset
are different object types, and I don't see this similarity between other iterable object types (ex. {1} == (1,)
evaluates to False
). Why does this behavior occur with sets? Are there other iterable object types that have similar behavior?
Python frozenset() Frozen set is just an immutable version of a Python set object. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation. Due to this, frozen sets can be used as keys in Dictionary or as elements of another set.
Overview. In Python, frozenset() is a function that takes an iterable and returns its frozenset object counterpart. A frozenset object is an immutable, unordered data type.
What is frozenset() in Python? frozenset() is a built-in function in Python that is used to return an immutable (unchangeable) object in Python. It accepts a single parameter that is an iterable and if it's mutable like a List or String, it returns an equivalent Frozenset object that is immutable.
As per the documentation python2 and documentation python3
Instances of
set
are compared to instances offrozenset
based on their members. For example, "set('abc') == frozenset('abc')
" returnsTrue
.
and in the python3 documentation:
Both set and frozenset support set to set comparisons. Two sets are equal if and only if every element of each set is contained in the other (each is a subset of the other). A set is less than another set if and only if the first set is a proper subset of the second set (is a subset, but is not equal). A set is greater than another set if and only if the first set is a proper superset of the second set (is a superset, but is not equal).
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