henrybbosa
henrybbosa

Reputation: 1125

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 set(iterable) function creates both a mutable set and an immutable frozenset. How do I know if I am creating a set or a frozenset?

Upvotes: 1

Views: 466

Answers (1)

miradulo
miradulo

Reputation: 29690

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.

Upvotes: 3

Related Questions