Freeze in Python?

前端 未结 2 1438
攒了一身酷
攒了一身酷 2021-01-04 06:52

I have programmed in Python for a while, and just recently started using Ruby at work. The languages are very similar. However, I just came across a Ruby feature that I don\

2条回答
  •  天涯浪人
    2021-01-04 07:29

    >>> a = [1,2,3]
    >>> a[1] = 'chicken'
    >>> a
    [1, 'chicken', 3]
    >>> a = tuple(a)
    >>> a[1] = 'tuna'
    Traceback (most recent call last):
      File "", line 1, in 
        a[1] = 'tuna'
    TypeError: 'tuple' object does not support item assignment
    

    Also, cf. set vs. frozenset, bytearray vs. bytes.

    Numbers, strings are immutable themselves:

    >>> a = 4
    >>> id(a)
    505408920
    >>> a = 42        # different object
    >>> id(a)
    505409528
    

提交回复
热议问题