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

Chris Angelico rosuav at gmail.com
Tue Jan 4 13:36:00 EST 2022


On Wed, Jan 5, 2022 at 5:29 AM Marco Sulla <Marco.Sulla.Python at gmail.com> wrote:
>
> $ python
> Python 3.10.0 (heads/3.10-dirty:f6e8b80d20, Nov 18 2021, 19:16:18)
> [GCC 10.1.1 20200718] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> a = {1:2}
> >>> c = {1:2, 3:4}
> >>> c.keys() - a.keys()
> {3}
> >>>
>

Let's start with this.

>>> a = {1}
>>> c = {1, 3}
>>> c - a
{3}

Do you agree that this should be a set?

If so, then the next question is: should the keys view be considered
frozen or not? Remember the set of keys can change (when the
underlying dict changes).

It's not difficult to construct a frozenset from a set.

ChrisA


More information about the Python-list mailing list