编程语言
首页 > 编程语言> > python中不可变集合

python中不可变集合

作者:互联网

 

1、

>>> a = {1,2,3}
>>> a
{1, 2, 3}
>>> type(a)
<class 'set'>
>>> a.add(4)
>>> a
{1, 2, 3, 4}
>>> b = frozenset({1,2,3})    ## 不可变集合
>>> b
frozenset({1, 2, 3})
>>> type(b)
<class 'frozenset'>
>>> b.add(4)
Traceback (most recent call last):
  File "<pyshell#845>", line 1, in <module>
    b.add(4)
AttributeError: 'frozenset' object has no attribute 'add'
>>> c = frozenset(a)     ## 不可变集合
>>> c
frozenset({1, 2, 3, 4})
>>> c.add(4)
Traceback (most recent call last):
  File "<pyshell#848>", line 1, in <module>
    c.add(4)
AttributeError: 'frozenset' object has no attribute 'add'

 

标签:last,python,attribute,不可,object,most,add,frozenset,集合
来源: https://www.cnblogs.com/liujiaxin2018/p/14473585.html