简体   繁体   English

Python中的不变性

[英]Immutability in Python

I trying to understand how the immutability works in python. 我试图理解不变性在python中是如何工作的。 Since string are immutable in python, I was expecting the id to change every time I perform a string operation but it doesn't work as expected. 因为字符串在python中是不可变的,所以我希望每次执行字符串操作时都会更改id,但它不能按预期工作。 example: The last operation on t doesn't change its id. 示例:t上的最后一个操作不会更改其ID。 Any ideas why? 有什么想法吗?

屏幕截图的操作

I had a row of apples in different cells [memory containing variables (I will not go to the bit level)] , of which some were empty [cells containing garbage / empty value] . 我在不同的单元格中有一排苹果[memory containing variables (I will not go to the bit level)] ,其中一些是空的[cells containing garbage / empty value]

I took one out. 我拿了一个。 It was in cell 3 [logical address = 3] . 它在单元格3中[logical address = 3]

I painted it blue (after I cloned it using future tech, for immutability demonstration) [committed an operation on it, same could go for addition for integers] . 我把它画成蓝色(在我使用未来的技术克隆它之后,用于不变性演示) [committed an operation on it, same could go for addition for integers]

I looked where to put it, and although cell 4 was free, cell 3 was also (because the "original" apple is not here anymore)! 我看着它放在哪里,虽然4号细胞是免费的,但是3号细胞也是(因为“原来的”苹果不再在这里了)! So I put it back in cell 3 [and although we get a "new" apple, it has the same address] . 所以我把它放回到单元格3 [and although we get a "new" apple, it has the same address]


Same goes for your t (note that id is the memory address of the variable in CPython), but since we are talking about "chains of apples" here (strings are made of a characters sequence, we have to consider the amount of space we have to continue the sequence, so if I had my memory looking like ( _ stands for arbitrary garbage data, '^' for space) 你的t (注意id是CPython中变量的内存地址),但是因为我们在这里谈论“苹果链”(字符串由字符序列组成,我们必须考虑我们的空间量)必须继续序列,所以如果我的内存看起来像( _代表任意垃圾数据,'^'代表空间)

H e l l o _ _ _ _ _ B O O M
^ string pointer points here

and I wanted to change the string to "Hello you" , I might consider using the free space: 我想将字符串更改为"Hello you" ,我可以考虑使用可用空间:

H e l l o ^ y o u _ B O O M
^ string pointer points here

But if I want to change the string to "Hello world!" 但是,如果我想将字符串更改为"Hello world!" , I would have to look for free space in the length of "Hello world!" ,我必须寻找"Hello world!"长度的自由空间"Hello world!" somewhere else (we might have it right after "BOOM" , which is probable in a garbage collected environment, look at how your IDs differs): 在其他地方(我们可能在"BOOM" ,在垃圾收集环境中很可能,看看你的ID如何不同):

H e l l o ^ y o u _ B O O M _ H e l l o ^ w o r l d ! _ G A R B A G E
                              ^ string pointer points here

Ids of objects can be reused, as long as the original object is no longer present. 只要原始对象不再存在,就可以重复使用对象的ID。 This is not specific to strings, but to all Python types. 这不是特定于字符串,而是特定于所有Python类型。 For the simplest example, you can examine the id of a simple object : 对于最简单的示例,您可以检查简单object的id:

>>> print id(object())
140437485756544
>>> print id(object())
140437485756544

However, if we retain a reference to the previous object, the id will not be reused: 但是,如果我们保留对前一个对象的引用,则不会重用id:

>>> a = object()
>>> id(a)
140437485756544
>>> b = object()
>>> id(b)
140437485756560

You can reproduce the same behavior in your tests with strings by appending the intermediate results (values in t ) to a list. 通过将中间结果( t值)附加到列表,可以在测试中使用字符串重现相同的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM