frozenset can be altered by |=

Marco Sulla Marco.Sulla.Python at gmail.com
Fri Nov 19 16:37:30 EST 2021


Mh. Now I'm thinking that I've done

a = "Marco "
a += "Sulla"

many times without bothering.

On Fri, 19 Nov 2021 at 22:22, Chris Angelico <rosuav at gmail.com> wrote:
>
> 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
> --
> https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list