Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does {1} == frozenset({1}) in Python?

Tags:

python

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?

like image 361
Joel Avatar asked Jul 18 '18 16:07

Joel


People also ask

What does Frozenset mean in Python?

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.

Is Frozenset a data type in Python?

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 and decorators in Python?

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.


1 Answers

As per the documentation python2 and documentation python3

Instances of set are compared to instances of frozenset based on their members. For example, "set('abc') == frozenset('abc')" returns True.

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).

like image 153
Andre Motta Avatar answered Nov 06 '22 15:11

Andre Motta