开发者

Chart of mutable versus immutable types

开发者 https://www.devze.com 2023-02-04 16:50 出处:网络
Is there a table or a chart somewhere online which shows what types (inbuilt) are mutable and immutable in开发者_如何学Python python?I am not sure of a chart, but basically:

Is there a table or a chart somewhere online which shows what types (inbuilt) are mutable and immutable in开发者_如何学Python python?


I am not sure of a chart, but basically:

Mutable:

list, dictionary, bytearray Note: bytearray is not a sequence though.

Immutable:

tuple, str

You can check for mutability with:

>>> import collections
>>> l = range(10)
>>> s = "Hello World"
>>> isinstance(l, collections.MutableSequence)
True
>>> isinstance(s, collections.MutableSequence)
False

For a dictionary (mapping):

>>> isinstance({}, collections.MutableMapping)
True
0

精彩评论

暂无评论...
验证码 换一张
取 消