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

查看:22
本文介绍了字符串是否在 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天全站免登陆