我对什么是不可变类型感到困惑。我知道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类,它没有标记错误。
首先,一个类是否有方法或者它的类结构是什么与可变性无关。
int和float是不可变的。如果我这样做
a = 1
a += 5
它在内存的第一行将名称a指向1。在第二行,它查找1,加5,得到6,然后把a指向内存中的6——它没有以任何方式把1变成6。同样的逻辑适用于下面的例子,使用其他不可变类型:
b = 'some string'
b += 'some other string'
c = ('some', 'tuple')
c += ('some', 'other', 'tuple')
对于可变类型,我可以做一些事情来改变它在内存中存储的值。:
d = [1, 2, 3]
我已经在内存中创建了1、2和3的位置列表。如果我这样做
e = d
我只是把e指向d指向的同一个列表。然后我可以这样做:
e += [4, 5]
e和d所在的列表也会被更新到内存中的位置为4和5。
如果我回到一个不可变类型,用一个元组来做:
f = (1, 2, 3)
g = f
g += (4, 5)
那么f仍然只指向原来的元组——你已经把g指向了一个全新的元组。
现在,用你的例子
class SortedKeyDict(dict):
def __new__(cls, val):
return dict.__new__(cls, val.clear())
你经过的地方
d = (('zheng-cai', 67), ('hui-jun', 68),('xin-yi', 2))
(这是一个元组的元组)作为val,你会得到一个错误,因为元组没有.clear()方法-你必须传递dict(d)作为val为它工作,在这种情况下,你会得到一个空的SortedKeyDict作为结果。
对象是否可变取决于它的类型。这与它是否具有某些方法无关,也与类层次结构无关。
用户定义的类型(即类)通常是可变的。有一些例外,比如不可变类型的简单子类。其他不可变类型包括一些内置类型,如int、float、tuple和str,以及一些用C实现的Python类。
《Python语言参考》中“数据模型”一章的一般解释:
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.
你必须理解Python将所有数据表示为对象。其中一些对象(如列表和字典)是可变的,这意味着您可以在不改变其标识的情况下更改其内容。其他对象,如整数、浮点数、字符串和元组是不能更改的对象。
一个简单的理解方法是看一下对象ID。
下面是一个不可变的字符串。你不能改变它的内容。如果您试图更改它,它将引发TypeError。同样,如果我们分配新内容,则创建一个新对象,而不是修改内容。
>>> s = "abc"
>>> id(s)
4702124
>>> s[0]
'a'
>>> s[0] = "o"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> s = "xyz"
>>> id(s)
4800100
>>> s += "uvw"
>>> id(s)
4800500
你可以用一个列表这样做,它不会改变对象的身份
>>> i = [1,2,3]
>>> id(i)
2146718700
>>> i[0]
1
>>> i[0] = 7
>>> id(i)
2146718700
要阅读更多关于Python的数据模型,你可以看看Python语言参考:
双重指控
斑马3套指控模型
什么?浮点数是不可变的?但我做不到
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]
可变对象必须至少有一个方法能够改变对象。例如,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的语言,比如Ruby)学习Python的,并且坚持用另一种语言来理解它,下面是人们通常会感到困惑的地方:
>>> a = 1
>>> a = 2 # I thought int was immutable, but I just changed it?!
在Python中,赋值不是突变。
在c++中,如果你写a = 2,你是在调用a.operator=(2),这将改变存储在a中的对象(如果a中没有存储对象,这是一个错误)。
在Python中,a = 2对存储在a;它只是意味着2现在存储在a中。(如果a中没有存储对象,也没关系。)
归根结底,这是更深层次区别的一部分。
在c++这样的语言中,变量是内存中的类型化位置。如果a是int型,这意味着它有4个字节,编译器知道它应该被解释为int型。当你令a = 2时,它改变了存储在这4个字节内存中的内容从0,0,0,1变成了0,0,0,2。如果在其他地方有另一个int变量,它有自己的4个字节。
A variable in a language like Python is a name for an object that has a life of its own. There's an object for the number 1, and another object for the number 2. And a isn't 4 bytes of memory that are represented as an int, it's just a name that points at the 1 object. It doesn't make sense for a = 2 to turn the number 1 into the number 2 (that would give any Python programmer way too much power to change the fundamental workings of the universe); what it does instead is just make a forget the 1 object and point at the 2 object instead.
如果赋值不是突变,那么什么是突变呢?
Calling a method that's documented to mutate, like a.append(b). (Note that these methods almost always return None). Immutable types do not have any such methods, mutable types usually do.
Assigning to a part of the object, like a.spam = b or a[0] = b. Immutable types do not allow assignment to attributes or elements, mutable types usually allow one or the other.
Sometimes using augmented assignment, like a += b, sometimes not. Mutable types usually mutate the value; immutable types never do, and give you a copy instead (they calculate a + b, then assign the result to a).
但如果赋值不是突变,那么对象的一部分赋值是如何突变的呢?这就是棘手的地方。a[0] = b不会改变[0](再次,不像c++),但它会改变a(不像c++,除非是间接的)。
这就是为什么最好不要尝试按照您习惯的语言来理解Python的语义,而是根据Python的语义来学习Python的语义。
一个类是不可变的,如果该类的每个对象在实例化时都有一个固定的值,以后不能更改
换句话说,要么改变变量(名称)的整个值,要么就不管它。
例子:
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
查看这个表格:
源
在我看来,你似乎在争论可变/不可变到底意味着什么。下面是一个简单的解释:
首先,我们需要一个解释的基础。
把你编程的任何东西都想象成一个虚拟对象,一个以二进制数字序列的形式保存在计算机内存中的东西。(不过,不要试图把这个想象得太困难。^^)现在在大多数计算机语言中,你不会直接处理这些二进制数,而是更多地使用二进制数的解释。
例如,你不会想到像0x110, 0xaf0278297319或类似的数字,而是你会想到像6这样的数字或像“Hello, world”这样的字符串。然而,这些数字或字符串是计算机内存中二进制数的一种解释。对于变量的任何值都是如此。
简而言之:我们不是用实际值编程,而是用实际二进制值的解释编程。
Now we do have interpretations that must not be changed for the sake of logic and other "neat stuff" while there are interpretations that may well be changed. For example think of the simulation of a city, in other words a program where there are many virtual objects and some of these are houses. Now may these virtual objects (the houses) be changed and can they still be considered to be the same houses? Well of course they can. Thus they are mutable: They can be changed without becoming a "completely" different object.
现在想想整数:它们也是虚拟对象(计算机内存中的二进制数序列)。如果我们改变其中一个,比如把6的值加1,它还是6吗?当然不是。因此任何整数都是不可变的。
所以:如果虚拟对象的任何变化意味着它实际上变成了另一个虚拟对象,那么它就被称为不可变的。
最后的话:
(1)永远不要把你在现实世界中可变和不可变的经验与某种语言的编程混淆在一起:
每种编程语言都有自己的定义,哪些对象可以静音,哪些对象不可以静音。
因此,虽然您现在可能理解了含义上的差异,但仍然需要学习每种编程语言的实际实现. ...的确,一种语言可能有一个目的,即6可能被削弱为7。然后,这将是相当疯狂或有趣的东西,就像平行宇宙的模拟
(2)这个解释当然是不科学的,它是为了帮助你掌握可变和不可变的区别。
在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
太奇怪了。
这个答案的目标是创建一个单独的地方来找到所有关于如何判断您正在处理的是突变/非突变(不可变/可变)的好想法,以及在可能的情况下如何处理它?有些时候,突变是不受欢迎的,python在这方面的行为可能会让来自其他语言的编码人员感到违反直觉。
根据@mina-gabriel的一篇有用的文章:
可能会有帮助的书籍:《Python中的数据结构和算法》
摘自那本书,列出了可变/不可变类型:
可变/不可变类型图像
分析以上并结合@arrakëën的文章:
什么不会意外改变?
标量(存储单个值的变量类型)不会意外变化
数值示例:int(), float(), complex()
有一些“可变序列”:
Str (), tuple(), frozenset(), bytes()
可以什么?
类似对象的列表(lists, dictionary, sets, bytearray())
这里的一篇文章也提到了类和类实例,但这可能取决于类继承了什么和/或它是如何构建的。
我所说的“意外”是指来自其他语言的程序员可能没有预料到这种行为(除了Ruby和其他一些“类似Python”的语言)。
在这个讨论中补充:
这种行为是一种优势,因为它可以防止您意外地用多个占用内存的大型数据结构的副本填充代码。但当这种情况不受欢迎时,我们该如何解决呢?
对于列表,简单的解决方案是构建一个新的列表,如下所示:
列表 2 = 列表(列表 1)
对于其他结构……解决方案可能更加棘手。一种方法是遍历元素并将它们添加到新的空数据结构(相同类型)。
当传入可变结构时,函数可以改变原始值。如何分辨?
There are some tests given on other comments on this thread but then there are comments indicating these tests are not full proof
object.function() is a method of the original object but only some of these mutate. If they return nothing, they probably do. One would expect .append() to mutate without testing it given its name. .union() returns the union of set1.union(set2) and does not mutate. When in doubt, the function can be checked for a return value. If return = None, it does not mutate.
sorted() might be a workaround in some cases. Since it returns a sorted version of the original, it can allow you to store a non-mutated copy before you start working on the original in other ways. However, this option assumes you don't care about the order of the original elements (if you do, you need to find another way). In contrast .sort() mutates the original (as one might expect).
非标准方法(以防有用):
在github上发现了这个,在MIT许可下发布:
Github仓库下:tobgu命名为:pyrsistent
它是什么:Python持久化数据结构代码,用于在不希望发生变化时代替核心数据结构
对于自定义类,@分号建议检查是否有__hash__函数,因为可变对象通常不应该有__hash__()函数。
这就是我目前在这个话题上所收集到的全部信息。欢迎提出其他意见、纠正意见等。谢谢。
可变和不可变对象之间的区别
定义
可变对象:创建后可以更改的对象。
不可变对象:创建后不能更改的对象。
在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
Mutable意味着它可以改变/变异。相反,不可改变。
有些Python数据类型是可变的,有些则不是。
让我们来看看哪些类型适合每个类别,并看一些例子。
可变的
在Python中有各种可变类型:
列表
dict
集
让我们看看下面关于列表的例子。
list = [1, 2, 3, 4, 5]
如果我执行以下操作来更改第一个元素
list[0] = '!'
#['!', '2', '3', '4', '5']
它工作得很好,因为列表是可变的。
如果我们考虑这个列表,它被改变了,然后给它赋值一个变量
y = list
如果我们改变列表中的一个元素,比如
list[0] = 'Hello'
#['Hello', '2', '3', '4', '5']
如果输出y,它就会给出
['Hello', '2', '3', '4', '5']
因为list和y都指向同一个列表,我们改变了列表。
不可变的
在一些编程语言中,可以定义一个常量,如下所示
const a = 10
如果调用,它会给出一个错误
a = 20
然而,这在Python中不存在。
然而,在Python中,有各种不可变类型:
没有一个
保龄球
int
浮动
str
元组
让我们看看下面关于字符串的例子。
取字符串a
a = 'abcd'
我们可以得到第一个元素
a[0]
#'a'
如果试图给第一个位置的元素赋一个新值
a[0] = '!'
它会给出一个错误
“str”对象不支持项赋值
当对一个字符串使用+=时,例如
a += 'e'
#'abcde'
它不会给出一个错误,因为它把a指向了一个不同的字符串。
这和下面一样
a = a + 'f'
不改变字符串。
不可变的优点和缺点
•内存中的空间从一开始就知道。它不需要额外的空间。
•通常,它会让事情更有效率。例如,查找字符串的len()要快得多,因为它是字符串对象的一部分。