future.types.newstr.newstr.str .__ mod__为什么在Python 2中返回unicode

问题描述 投票:0回答:1

我正在同时致力于对Python 2和3的支持,但有些奇怪的事情到目前为止我还无法理解。

我正在使用python-future模块以实现此转换。当我在__mod__对象上使用newstr函数时,我的输出是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个回答
1
投票

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

文档说:

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

这说明了您得到的结果。

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

© www.soinside.com 2019 - 2024. All rights reserved.