frozenset can be altered by |=

Chris Angelico rosuav at gmail.com
Fri Nov 19 16:17:22 EST 2021


On Sat, Nov 20, 2021 at 8:16 AM Chris Angelico <rosuav at gmail.com> wrote:
>
> On Sat, Nov 20, 2021 at 8:13 AM Marco Sulla
> <Marco.Sulla.Python at gmail.com> wrote:
> >
> > (venv_3_10) marco at buzz:~$ 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 = frozenset((3, 4))
> > >>> a
> > frozenset({3, 4})
> > >>> a |= {5,}
> > >>> a
> > frozenset({3, 4, 5})
>
> That's the same as how "x = 4; x += 1" can "alter" four into five.
>
> >>> a = frozenset((3, 4))
> >>> id(a), a
> (140545764976096, frozenset({3, 4}))
> >>> a |= {5,}
> >>> id(a), a
> (140545763014944, frozenset({3, 4, 5}))
>
> It's a different frozenset.
>

Oh, even better test:

>>> a = frozenset((3, 4)); b = a
>>> id(a), a, id(b), b
(140602825123296, frozenset({3, 4}), 140602825123296, frozenset({3, 4}))
>>> a |= {5,}
>>> id(a), a, id(b), b
(140602825254144, frozenset({3, 4, 5}), 140602825123296, frozenset({3, 4}))

ChrisA


More information about the Python-list mailing list