我对什么是不可变类型感到困惑。我知道float对象被认为是不可变的,我的书中有这样的例子:
class RoundFloat(float):
def __new__(cls, val):
return float.__new__(cls, round(val, 2))
因为类结构/层次结构,这被认为是不可变的吗?,这意味着float位于类的顶部,是它自己的方法调用。类似于这种类型的例子(即使我的书说dict是可变的):
class SortedKeyDict(dict):
def __new__(cls, val):
return dict.__new__(cls, val.clear())
然而,可变的东西在类中有方法,例如:
class SortedKeyDict_a(dict):
def example(self):
return self.keys()
同样,对于最后一个类(SortedKeyDict_a),如果我将这种类型的set传递给它:
d = (('zheng-cai', 67), ('hui-jun', 68),('xin-yi', 2))
不调用示例方法,它将返回一个字典。带有__new__的SortedKeyDict将其标记为错误。我尝试用__new__将整数传递给RoundFloat类,它没有标记错误。
在Python中,有一种简单的方法可以知道:
不变的:
>>> s='asd'
>>> s is 'asd'
True
>>> s=None
>>> s is None
True
>>> s=123
>>> s is 123
True
可变的:
>>> s={}
>>> s is {}
False
>>> {} is {}
Flase
>>> s=[1,2]
>>> s is [1,2]
False
>>> s=(1,2)
>>> s is (1,2)
False
And:
>>> s=abs
>>> s is abs
True
所以我认为内置函数在Python中也是不可变的。
但我真的不明白float是如何工作的:
>>> s=12.3
>>> s is 12.3
False
>>> 12.3 is 12.3
True
>>> s == 12.3
True
>>> id(12.3)
140241478380112
>>> id(s)
140241478380256
>>> s=12.3
>>> id(s)
140241478380112
>>> id(12.3)
140241478380256
>>> id(12.3)
140241478380256
太奇怪了。
可变对象必须至少有一个方法能够改变对象。例如,list对象有append方法,它实际上会改变对象:
>>> a = [1,2,3]
>>> a.append('hello') # `a` has mutated but is still the same object
>>> a
[1, 2, 3, 'hello']
但是float类没有改变float对象的方法。你可以:
>>> b = 5.0
>>> b = b + 0.1
>>> b
5.1
但是=操作数不是一个方法。它只是在变量和它右边的东西之间做了一个绑定,没有别的。它从不改变或创建对象。它声明了变量将指向什么,从现在开始。
当执行b = b + 0.1时,=操作数将变量绑定到一个新的浮点数,其创建结果为5 + 0.1。
当你将一个变量赋值给一个存在的对象时,不管是否是可变的,=操作数将变量绑定到该对象。没有别的事情发生
在任何一种情况下,=都只是进行绑定。它不改变或创建对象。
当执行= 1.0时,=操作数创建的不是浮点数,而是该行的1.0部分。实际上,当您编写1.0时,它是float(1.0)的简写,一个返回float对象的构造函数调用。(这就是为什么如果你输入1.0并按enter键,你会得到下面打印的“echo”1.0;这是你调用的构造函数的返回值)
现在,如果b是一个浮点数,你赋值a = b,两个变量都指向同一个对象,但实际上变量之间不能通信,因为对象是不可变的,如果你做b += 1,现在b指向一个新对象,而a仍然指向旧对象,不知道b指向什么。
但如果c是,假设,一个列表,你赋值a = c,现在a和c可以“通信”,因为list是可变的,如果你执行c.append('msg'),然后检查a,你得到消息。
(顺便说一下,每个对象都有一个唯一的关联id号,你可以通过id(x)获得。所以你可以检查一个对象是否相同,或者不检查它的唯一id是否改变了。)
可变和不可变对象之间的区别
定义
可变对象:创建后可以更改的对象。
不可变对象:创建后不能更改的对象。
在Python中,如果你改变了不可变对象的值,它会创建一个新对象。
可变的对象
下面是Python中可变类型的对象:
列表
字典
集
中bytearray
用户定义的类
不可变对象
以下是Python中不可变类型的对象:
int
浮动
小数
复杂的
保龄球
字符串
元组
范围
frozenset
字节
一些悬而未决的问题
问:字符串是不可变类型吗?
回答:是的,但你能解释一下吗?
证据1:
a = "Hello"
a +=" World"
print a
输出
"Hello World"
在上面的例子中,字符串被创建为“Hello”,然后更改为“Hello World”。这意味着字符串是可变类型的。但当我们检查它的标识,看它是否为可变类型时,就不是这样了。
a = "Hello"
identity_a = id(a)
a += " World"
new_identity_a = id(a)
if identity_a != new_identity_a:
print "String is Immutable"
输出
String is Immutable
证据2:
a = "Hello World"
a[0] = "M"
输出
TypeError 'str' object does not support item assignment
问:元组是不可变类型吗?
答案:是的。
证据1:
tuple_a = (1,)
tuple_a[0] = (2,)
print a
输出
'tuple' object does not support item assignment
什么?浮点数是不可变的?但我做不到
x = 5.0
x += 7.0
print x # 12.0
那不是"mut" x吗?
你同意字符串是不可变的,对吧?但你可以做同样的事情。
s = 'foo'
s += 'bar'
print s # foobar
变量的值会改变,但改变的方式是改变变量所指向的对象。可变类型可以这样改变,也可以“就地”改变。
区别就在这里。
x = something # immutable type
print x
func(x)
print x # prints the same thing
x = something # mutable type
print x
func(x)
print x # might print something different
x = something # immutable type
y = x
print x
# some statement that operates on y
print x # prints the same thing
x = something # mutable type
y = x
print x
# some statement that operates on y
print x # might print something different
具体的例子
x = 'foo'
y = x
print x # foo
y += 'bar'
print x # foo
x = [1, 2, 3]
y = x
print x # [1, 2, 3]
y += [3, 2, 1]
print x # [1, 2, 3, 3, 2, 1]
def func(val):
val += 'bar'
x = 'foo'
print x # foo
func(x)
print x # foo
def func(val):
val += [3, 2, 1]
x = [1, 2, 3]
print x # [1, 2, 3]
func(x)
print x # [1, 2, 3, 3, 2, 1]
一个类是不可变的,如果该类的每个对象在实例化时都有一个固定的值,以后不能更改
换句话说,要么改变变量(名称)的整个值,要么就不管它。
例子:
my_string = "Hello world"
my_string[0] = "h"
print my_string
你希望这可以工作并打印hello world,但这将抛出以下错误:
Traceback (most recent call last):
File "test.py", line 4, in <module>
my_string[0] = "h"
TypeError: 'str' object does not support item assignment
解释器说:我不能改变这个字符串的第一个字符
你将不得不改变整个字符串,以使其工作:
my_string = "Hello World"
my_string = "hello world"
print my_string #hello world
查看这个表格:
源