Why operations between dict views return a set and not a frozenset?

Cameron Simpson cs at cskk.id.au
Tue Jan 4 16:30:30 EST 2022


On 04Jan2022 21:03, Marco Sulla <Marco.Sulla.Python at gmail.com> wrote:
>On Tue, 4 Jan 2022 at 19:38, Chris Angelico <rosuav at gmail.com> wrote:
>> [...] should the keys view be considered
>> frozen or not? Remember the set of keys can change (when the
>> underlying dict changes).
>
>Well, also the items can change, but they are returned as tuples with
>2 elements.
>
>It seems to me that the stdlib, when something should return a
>sequence, prefers to return a tuple. So I expected the same preference
>for frozenset over set.
>
>> It's not difficult to construct a frozenset from a set.
>
>This sentence has the commutative property :)

Indeed.

But speaking for myself, I may well want to perform additional work on 
the object returned. Making a copy of it for tht purpose seems very 
wasteful (imagine the set is quite large). A modifiable version can be 
used immediately with no time or space cost. And it can be left alone if 
it is to be unchanged.  If I got a frozenset back I would inherently 
have to copy it to do "modifying work".

So I prefer getting a modifiable object back.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list