【问题标题】:Why does future.types.newstr.newstr.str.__mod__ return Unicode?为什么 future.types.newstr.newstr.str.__mod__ 返回 Unicode?
【发布时间】:2020-01-02 16:59:23
【问题描述】:

我正在同时致力于 Python 2 和 3 的支持,但我有一些奇怪的事情我至今无法理解。

我正在使用python-future 模块来实现这种转换。当我在newstr 对象上使用__mod__ 函数时,我的输出是unicode,而我希望它是newstr。我错过了什么吗?

>>> from builtins import str # provided by the future module to allow smooth transition from Py2 to Py3
>>> str('%s %s') % (u'Hello', u'World')
u'Hello World'
>>> type(str('%s %s') % (u'Hello', u'World'))
<type 'unicode'>

【问题讨论】:

    标签: python python-2.x python-2to3


    【解决方案1】:

    % 是 Python 2 的内置运算符,除非重载,否则不了解 python-future 模块。

    文档说:

    如果提供的对象或格式是 unicode 字符串,则生成的字符串也将是 unicode。

    这解释了你得到的结果。

    更多信息: https://docs.python.org/2/library/stdtypes.html#string-formatting

    【讨论】:

    • 第一句话可能不正确/不相关。 +1 其余部分。
    • 谢谢,我已经改了句,让它更相关/更准确
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多