python字符串加空格_带有空格的Python字符串,最后没有空格和不...

这是CPython实现如何选择缓存字符串文字的一个怪癖.具有相同内容的字符串文字可以引用相同的字符串对象,但它们不必.当’string’不是因为’string’只包含Python标识符中允许的字符时,’string’碰巧会自动被中断.我不知道为什么这是他们选择的标准,但确实如此.在不同的Python版本或实现中,行为可能不同.

从CPython 2.7源代码,stringobject.h,第28行:

Interning strings (ob_sstate) tries to ensure that only one string

object with a given value exists, so equality tests can be one pointer

comparison. This is generally restricted to strings that “look like”

Python identifiers, although the intern() builtin can be used to force

interning of any string.

/* Intern selected string constants */

for (i = PyTuple_Size(consts); --i >= 0; ) {

PyObject *v = PyTuple_GetItem(consts, i);

if (!PyString_Check(v))

continue;

if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))

continue;

PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));

}

另请注意,实习是与Python字节码编译器合并字符串文字的单独过程.如果让编译器一起编译a和b分配,例如通过将它们放在模块或if True:中,您会发现a和b将是相同的字符串.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值