是否空字符串缓存? [英] Is empty string cached?

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

问题描述

当我将一个空字符串传递给一个函数是一个新的字符串对象创建

还是python使用一些全局预创建的对象?我知道python确实

这个整数对象在一定值下。例如,在

下面的代码是为每个函数调用创建的新字符串对象?


func(0,'''')

func(1,'''')

func(2,'''')

func(3,''')

x =''''
y =''''
x是y
真x =''你好''
y =''你好''
x是y



正确


这让我相信python会重用现有的字符串,但是一旦删除变量就会获得
,该项目仍然存在于缓存中?


-Farshid

解决方案

Farshid Lashkari写道:

当我将一个空字符串传递给一个函数是一个新的字符串对象创建
或python是否使用一些全局预创建的对象?我知道python在某个值下使用整数对象。例如,在下面的代码中是为每个函数调用创建的新字符串对象?

func(0,'''')
func(1,''' ')
func(2,'''')
func(3,'''')



在这种情况下,语言实现可以创建新的

字符串或重用现有字符串:


表示不可变类型,计算新值的操作

可能实际上返回对任何现有对象的引用,其中

的类型和值相同,而对于可变对象,这是不允许的。

[ http://docs.python.org/ref/objects.html]

[...]这让我相信python会重用现有的字符串,但是一旦删除了变量,该项目是否仍然存在于缓存中?




要么;看到相同的参考页面。

-

--Bryan


Farshid Lashkari写道:

当我将一个空字符串传递给一个函数是一个新的字符串对象创建
或python是否使用了一些全局预创建的对象?我知道python在某个值下使用整数对象。例如,在下面的代码中是为每个函数调用创建的新字符串对象?

func(0,'''')
func(1,''' ')
func(2,'''')
func(3,'''')

我在交互式shell中尝试了以下命令:

>> x =''''
>> y =''''
>> x是y True>> x =''你好''
>> y =''你好''
>> x是y True

这让我相信python会重用现有的字符串,但是一旦删除了变量,该项目是否仍然存在于缓存中?



诱导信念的证据太少:

a =" hello"
b =" h" +" ello"
a是b
False c =" hello"
b是
False




问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC www.holdenweb.com

PyCon TX 2006 www.python.org/pycon/


>引用信念的证据太少:

>>> a =" hello"
>>> b =" h" +" ello"
>>> a是b False>>> c =" hello"
>>> b是假的>>>


我不明白你最后一句话的重点。你打算改为

这个:

c是


真的


但是,以下命令加上我的困惑:

a =''wtf?''
b =''wtf?''
a是b


错误


那么如何缓存字符串文字?某处有解释吗?

它是一些怪异的伏都教,我应该假设一个字符串文字

将始终生成一个新对象?


谢谢,

Farshid


When I pass an empty string to a function is a new string object created
or does python use some global pre-created object? I know python does
this with integer objects under a certain value. For instance, in the
following code is a new string object created for each function call?

func(0,'''')
func(1,'''')
func(2,'''')
func(3,'''')

I tried the following commands in the interactive shell:

x = ''''
y = ''''
x is y True x = ''hello''
y = ''hello''
x is y


True

This leads me to believe that python does reuse existing strings, but
once the variables are removed, does the item still exist in the cache?

-Farshid

解决方案

Farshid Lashkari wrote:

When I pass an empty string to a function is a new string object created
or does python use some global pre-created object? I know python does
this with integer objects under a certain value. For instance, in the
following code is a new string object created for each function call?

func(0,'''')
func(1,'''')
func(2,'''')
func(3,'''')

In this case, the language implementation may either create new
strings or re-use existing ones:

for immutable types, operations that compute new values
may actually return a reference to any existing object with
the same type and value, while for mutable objects this is
not allowed.
[http://docs.python.org/ref/objects.html]
[...] This leads me to believe that python does reuse existing strings, but
once the variables are removed, does the item still exist in the cache?



Either; see the same reference page.
--
--Bryan


Farshid Lashkari wrote:

When I pass an empty string to a function is a new string object created
or does python use some global pre-created object? I know python does
this with integer objects under a certain value. For instance, in the
following code is a new string object created for each function call?

func(0,'''')
func(1,'''')
func(2,'''')
func(3,'''')

I tried the following commands in the interactive shell:

>> x = ''''
>> y = ''''
>> x is y True >> x = ''hello''
>> y = ''hello''
>> x is y True

This leads me to believe that python does reuse existing strings, but
once the variables are removed, does the item still exist in the cache?


It takes far too little evidence to induce belief:

a = "hello"
b = "h"+"ello"
a is b False c = "hello"
b is a False



regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


> It takes far too little evidence to induce belief:

>>> a = "hello"
>>> b = "h"+"ello"
>>> a is b False >>> c = "hello"
>>> b is a False >>>
I don''t understand the point of your last expression. Were you intending
this instead:
c is a

True

However, the following commands add to my confusion:
a = ''wtf?''
b = ''wtf?''
a is b


False

So how are string literals cached? Is there an explanation somewhere? Is
it some freaky voodoo, and I should just assume that a string literal
will always generate a new object?

Thanks,
Farshid


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

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