What does Python treat as reference types?

后端 未结 3 2025
日久生厌
日久生厌 2020-11-29 06:35

I assumed sequence types in Python were value types. It turns out they\'re reference types (Meaning that the value of a variable won\'t be copied when assigned to a new vari

3条回答
  •  -上瘾入骨i
    2020-11-29 07:15

    All values in Python are references. What you need to worry about is if a type is mutable. The basic numeric and string types, as well as tuple and frozenset are immutable; names that are bound to an object of one of those types can only be rebound, not mutated.

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

提交回复
热议问题