Difference between tuples and frozensets in Python

后端 未结 4 1644
面向向阳花
面向向阳花 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:32

    Volatility does mention that frozensets are not indexed. I was looking at the other functionality, so did not immediately realize that standard python slicing is not possible.

    a = frozenset((1, 1, 1, 1, 2, 2, 2))  # results in frozenset([1, 2])
    print a[0] 
    

    will give error:

    TypeError: 'frozenset' object does not support indexing
    

    Obvious from fact that it is not indexed, but though it was worth adding explicitly here

提交回复
热议问题