字符串是否在Python中合并? [英] Are strings pooled in Python?

查看:78
本文介绍了字符串是否在Python中合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python是否有一个包含所有字符串的池,并且在那里它们(字符串)是单例的吗?

Does Python have a pool of all strings and are they (strings) singletons there?

更准确地说,在下面的代码中,是在内存中创建一个还是两个字符串?

More precise, in the following code, are one or two strings created in memory?

a = str(num)
b = str(num)

推荐答案

字符串在Python中是不可变的,因此实现可以决定是否进行实习(这是经常与C#相关联的术语,这意味着某些字符串存储在池中)是否输入字符串.

Strings are immutable in Python, so the implementation can decide whether to intern (that's a term often associated with C#, meaning that some strings are stored in a pool) strings or not.

在您的示例中,您正在动态创建字符串. CPython并非总是 进入池中以检测字符串是否已经存在-这也没有意义,因为您必须先保留内存才能创建字符串,然后将其与池内容(对于长字符串而言效率不高).

In your example, you're dynamically creating strings. CPython does not always look into the pool to detect whether the string is already there - it also doesn't make sense because you first have to reserve memory in order to create the string, and then compare it to the pool content (inefficient for long strings).

但是对于长度为1的字符串,CPython确实会向池中查找(参见"stringobject.c"):

But for strings of length 1, CPython does look into the pool (cf. "stringobject.c"):

static PyStringObject *characters[UCHAR_MAX + 1];

...

PyObject *
PyString_FromStringAndSize(const char *str, Py_ssize_t size)
{

...

    if (size == 1 && str != NULL &&
    (op = characters[*str & UCHAR_MAX]) != NULL)
    {
        #ifdef COUNT_ALLOCS
            one_strings++;
        #endif

        Py_INCREF(op);
        return (PyObject *)op;
    }

...

所以:

a = str(num)
b = str(num)
print a is b # <-- this will print False in most cases (but try str(1) is str(1))

但是当直接在代码中使用 constant 字符串时,CPython使用相同的字符串实例:

But when using constant strings directly in your code, CPython uses the same string instance:

a = "text"
b = "text"
print a is b # <-- this will print True

这篇关于字符串是否在Python中合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆