Are Python sets mutable?

前端 未结 8 1932
渐次进展
渐次进展 2021-01-31 09:14

Are sets in Python mutable?


In other words, if I do this:

x = set([1, 2, 3])
y = x

y |= set([4, 5, 6])

Are x and

8条回答
  •  抹茶落季
    2021-01-31 09:40

    After changing the set, even their object references match. I don't know why that textbook says sets are immutable.

        >>> s1 ={1,2,3}
        >>> id(s1)
        140061513171016
        >>> s1|={5,6,7}
        >>> s1
        {1, 2, 3, 5, 6, 7}
        >>> id(s1)
        140061513171016
    

提交回复
热议问题