Tariiq Henry Bbosa Tariiq Henry Bbosa - 7 months ago 49
Python Question

How to know when creating set or frozenset

I am new to Python. I am reading Building Skills in Python (Lott) and trying out some examples. I see that the

function creates both a mutable set and an immutable frozenset. How do I know if I am creating a set or a frozenset?


That is simply incorrect. The set() built-in returns a set, not a frozenset. frozenset() returns a frozenset. A set and a frozenset are both set types, however they are distinct set types.

The Python docs can always be useful for clarification on things like this, there's an entire list of built-in functions.

Excerpt from the book Building Skills in Python (Lott) noted by OP in a comment, emphasis mine.

A set value is created by using the set() or frozenset() factory functions. These can be applied to any iterable container, which includes any sequence, the keys of a dict, or even a file.

The author here is using "set value" to describe a value of set type, and is thus not indicating that set() and frozenset() do the same thing - they produce values of distinct set types, namely sets and frozensets.