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

Chris Angelico rosuav at gmail.com
Mon Jan 10 19:03:11 EST 2022


On Tue, Jan 11, 2022 at 10:26 AM Marco Sulla
<Marco.Sulla.Python at gmail.com> wrote:
>
> On Wed, 5 Jan 2022 at 23:02, Chris Angelico <rosuav at gmail.com> wrote:
> >
> > On Thu, Jan 6, 2022 at 8:01 AM Marco Sulla <Marco.Sulla.Python at gmail.com> wrote:
> > >
> > > On Wed, 5 Jan 2022 at 14:16, Chris Angelico <rosuav at gmail.com> wrote:
> > > > That's an entirely invisible optimization, but it's more than just
> > > > "frozenset is faster than set". It's that a frozenset or tuple can be
> > > > stored as a function's constants, which is a massive difference.
> > >
> > > Can you explain this?
> >
> > Play around with dis.dis and timeit.
>
> ? I don't understand. You're talking about function constants. What
> are they? I can't dig deep into something if I can't know what it is.
> Maybe are you talking about function default values for parameters?

No, I'm talking about constants. Every function has them.

> Of course. You can use a proxy and slow down almost everything much
> more. Or you can simply create a version of the mutable object with
> fewer methods, as more or less frozenset is. I checked the
> implementation, no fast iteration is implemented. I do not understand
> why in `for x in {1, 2, 3}` the set is substituted by a frozenset.

Constants. Like I said, play around with dis.dis, and explore what's
already happening. A set can't be a constant, a frozenset can be.
Constants are way faster than building from scratch.

Explore. Play around. I'm not going to try to explain everything in detail.

If you're delving into the details of the C implementation of the
dictionary, I would have expected you'd already be familiar with the
way that functions behave.

ChrisA


More information about the Python-list mailing list