frozenset can be altered by |=

Marco Sulla Marco.Sulla.Python at gmail.com
Mon Nov 29 16:53:48 EST 2021


I must say that I'm reading the documentation now, and it's a bit
confusing. In the docs, inplace operators as |= should not work. They
are listed under the set-only functions and operators. But, as we saw,
this is not completely true: they work but they don't mutate the
original object. The same for += and *= that are listed under `list`
only.

On Mon, 22 Nov 2021 at 19:54, Marco Sulla <Marco.Sulla.Python at gmail.com> wrote:
>
> Yes, and you do this regularly. Indeed integers, for example, are immutables and
>
> a = 0
> a += 1
>
> is something you do dozens of times, and you simply don't think that
> another object is created and substituted for the variable named `a`.
>
> On Mon, 22 Nov 2021 at 14:59, Chris Angelico <rosuav at gmail.com> wrote:
> >
> > On Tue, Nov 23, 2021 at 12:52 AM David Raymond <David.Raymond at tomtom.com> wrote:
> > > It is a little confusing since the docs list this in a section that says they don't apply to frozensets, and lists the two versions next to each other as the same thing.
> > >
> > > https://docs.python.org/3.9/library/stdtypes.html#set-types-set-frozenset
> > >
> > > The following table lists operations available for set that do not apply to immutable instances of frozenset:
> > >
> > > update(*others)
> > > set |= other | ...
> > >
> > >     Update the set, adding elements from all others.
> >
> > Yeah, it's a little confusing, but at the language level, something
> > that doesn't support |= will implicitly support it using the expanded
> > version:
> >
> > a |= b
> > a = a | b
> >
> > and in the section above, you can see that frozensets DO support the
> > Or operator.
> >
> > By not having specific behaviour on the |= operator, frozensets
> > implicitly fall back on this default.
> >
> > ChrisA
> > --
> > https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list