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
精彩评论