问题描述
在Java中,显式声明的字符串由JVM进行插值,因此对同一字符串的后续声明将导致指向同一String实例的两个指针,而不是两个单独的(但相同的)字符串.
In Java, explicitly declared Strings are interned by the JVM, so that subsequent declarations of the same String results in two pointers to the same String instance, rather than two separate (but identical) Strings.
例如:
public String baz() {
String a = "astring";
return a;
}
public String bar() {
String b = "astring"
return b;
}
public void main() {
String a = baz()
String b = bar()
assert(a == b) // passes
}
我的问题是,CPython(或任何其他Python运行时)是否对字符串做相同的事情?例如,如果我有一些课程:
My question is, does CPython (or any other Python runtime) do the same thing for strings? For example, if I have some class:
class example():
def __init__():
self._inst = 'instance'
并创建10个此类的实例,是否每个实例都有一个实例变量,该实例变量引用内存中的同一字符串,或者我将得到10个单独的字符串?
And create 10 instances of this class, will each one of them have an instance variable referring to the same string in memory, or will I end up with 10 separate strings?
推荐答案
这称为Interning,是的,Python会在某种程度上这样做,以较短的字符串形式创建为字符串文字.请参阅关于不可变字符串的更改ID 一些讨论.
This is called interning, and yes, Python does do this to some extent, for shorter strings created as string literals. See About the changing id of an immutable string for some discussion.
实习是依赖于运行时的,它没有标准.实习总是在内存使用和检查是否要创建相同字符串的成本之间进行权衡.如果 some 文档会自动为您完成:
Interning is runtime dependent, there is no standard for it. Interning is always a trade-off between memory use and the cost of checking if you are creating the same string. There is the sys.intern()
function to force the issue if you are so inclined, which documents some of the interning Python does for you automatically:
请注意,Python 2的intern()
函数曾经是内置函数,无需导入.
Note that Python 2 the intern()
function used to be a built-in, no import necessary.
这篇关于Python会实习生字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!