Difference between tuples and frozensets in Python

后端 未结 4 1660
面向向阳花
面向向阳花 2020-12-29 00:49

I\'m learning Python 3 using The Quick Python Book, where the author talks about frozensets, stating that since sets are mutable and hence unhashable, thereby becoming unfit

4条回答
  •  鱼传尺愫
    2020-12-29 01:41

    Somewhat counter intuitive - what about this bon mot:

    sss = frozenset('abc')
    sss |= set('efg')
    

    Will yield:

    frozenset(['a', 'c', 'b', 'e', 'g', 'f'])
    

    Of course, this is equivalent to x = x | y, so not changing the original frozenset, but it doesn't half make a mockery of the term 'immutable' to the code reviewer!

提交回复
热议问题