我正在同时致力于对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 2的内置运算符,除非重载,否则不了解python-future模块。
文档说:
如果提供的对象或格式是unicode字符串,则生成的字符串也将是unicode。
这说明了您得到的结果。
更多信息:https://docs.python.org/2/library/stdtypes.html#string-formatting