frozendict: an experiment
Inada Naoki
songofacandy at gmail.com
Wed Jul 15 02:07:07 EDT 2020
On Wed, Jul 15, 2020 at 2:01 AM Marco Sulla
<Marco.Sulla.Python at gmail.com> wrote:
>
> > Why do you think I do not need views to use the frozendict?
> > I thought that is what make d.key(), d.items() etc work?
>
> Views for dict exist because dict is mutable. See this example:
>
> >>> d = {1: 2}
> >>> keys = d.keys()
> >>> d[2] = 3
> >>> keys
> dict_keys([1, 2])
>
> As you see, even if you modified the dict __after__ you created the
> keys view, the view returns also the new key, 2.
> This is desirable for a mutable object, but it's not needed for an
> immutable one. frozendict could create lazily an object that contains
> all its keys and cache it.
I don't think so. The view objects are useful when we need a set-like
operation. (e.g. `assert d.keys() == {"spam", "egg"}`)
There is no difference between mutable and immutable dicts.
--
Inada Naoki <songofacandy at gmail.com>
More information about the Python-list
mailing list