frozenset can be altered by |=

MRAB python at mrabarnett.plus.com
Fri Nov 19 16:50:24 EST 2021


On 2021-11-19 21:11, Marco Sulla 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})
> 
I'll counter with:

Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> a = frozenset((3, 4))
 >>> a
frozenset({3, 4})
 >>> b = a
 >>> a |= {5,}
 >>> a
frozenset({3, 4, 5})
 >>> b
frozenset({3, 4})


frozenset doesn't support mutation, so:

     a |= {5,}

falls back to:

     a = a | {5,}


The same kind of thing happens with tuples:

 >>> a = (3, 4)
 >>> a
(3, 4)
 >>> a += (5,)
 >>> a
(3, 4, 5)


More information about the Python-list mailing list