Python 元组"可变"与"不可变"
in 代码编程 with 0 comment

Python 元组"可变"与"不可变"

in 代码编程 with 0 comment

tuple and list

tuple 中包含list list 可以更改
https://docs.python.org/3.3/reference/datamodel.html#objects-values-and-types

The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable. (The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.) An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable.

WX20190730-151005.png

tuple1 = (1,2,[])
tuple1[2].append(11)
tuple1
(1, 2, [11])
回复