unicode_literals和type() [英] unicode_literals and type()
问题描述
我在type()
调用上支持python2和python3时遇到问题.这说明了问题:
I'm having problems supporting python2 and python3 on a type()
call. This demonstrates the problem:
from __future__ import unicode_literals
name='FooClass'
type(name, (dict,), {})
在python3上没有问题,但是在python2上:
No problem on python3, but on python2:
Traceback (most recent call last):
File "test.py", line 6, in <module>
type(name, (dict,), {})
TypeError: type() argument 1 must be string, not unicode
这与在Python 2.6中使用unicode_literals的任何陷阱有关?.在该问题中,有人建议将类型转换为字节串,因此我天真地考虑使用 six.b()
:
This is related to Any gotchas using unicode_literals in Python 2.6?. In that question, someone recommends typecasting to a bytestring, so naively I thought about using six.b()
:
假"字节文字.数据应始终为常规字符串文字. 在Python 2中,b()返回一个8位字符串.在Python 3中,数据已编码 将latin-1编码为字节.
A "fake" bytes literal. data should always be a normal string literal. In Python 2, b() returns a 8-bit string. In Python 3, data is encoded with the latin-1 encoding to bytes.
所以看起来像这样:
from __future__ import unicode_literals
import six
name='FooClass'
type(six.b(name), (dict,), {})
但是在python2和python3上均失败:
But it fails on both python2 and python3:
$ python2 test.py
Traceback (most recent call last):
File "test.py", line 6, in <module>
type(six.b(name), (dict,), {})
TypeError: type() argument 1 must be string, not unicode
$ python3 test.py
Traceback (most recent call last):
File "test.py", line 6, in <module>
type(six.b(name), (dict,), {})
TypeError: type() argument 1 must be str, not bytes
所以看来真的,type()
想要一个python2 str,它是python2上的python3字节串,但是它想要一个python3 str,它是python3上的python2 unicode字符串.
So it seems that really, type()
wants a python2 str which is a python3 bytestring on python2, but it wants a python3 str which is a python2 unicode string on python3.
您怎么看?
有什么我不理解的东西吗?
Is there something I don't understand ?
还是在python 2和3上与type()
真正不兼容?
Or is there a real incompatibility with type()
on python 2 and 3 ?
没有任何方法来进行相同的type()
调用,同时支持2和3吗?
Isn't there any way to have the same type()
call supporting both 2 and 3 ?
在这种情况下,像six
这样的工具是否应该提供对type()
的包装吗?
Shouldn't a tool like six
provide a wrapper around type()
in that case ?
推荐答案
six.b
是在您不使用unicode_literals
(并且将字符串文字作为文档传递给您)的前提下编写的状态),因此Python 2实现只是def b(s): return s
,因为Python 2字符串文字已经是字节字符串.
six.b
is written under the assumption that you won't use unicode_literals
(and that you'll pass a string literal to it, as the documentation states), so the Python 2 implementation is just def b(s): return s
as a Python 2 string literal is already a byte string.
在此模块中不要使用unicode_literals
,或使用(如注释所示)str(name)
.在Python 3中,这是无操作的.在Python 2中,它无声地将unicode字符串转换为字节字符串(假设有些编码我不介意记住,但是它是ASCII的超集,所以应该没问题).
Either don't use unicode_literals
in this module, or use (as a comment suggests) str(name)
. In Python 3, that is a no-op. In Python 2, it silently converts the unicode string to a byte string (assuming some encoding that I can't be bothered to remember, but it's a superset of ASCII so you should be fine).
这篇关于unicode_literals和type()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!