frozenset can be altered by |=

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


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.

ChrisA


More information about the Python-list mailing list