【发布时间】:2013-11-06 05:39:26
【问题描述】:
我在type() 调用中支持 python2 和 python3 时遇到问题。这说明了问题:
from __future__ import unicode_literals
name='FooClass'
type(name, (dict,), {})
在python3上没问题,但是在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
这与Any gotchas using unicode_literals in Python 2.6? 有关。在那个问题中,有人建议将类型转换为字节串,所以我天真地考虑使用six.b():
“假”字节文字。 data 应该始终是一个普通的字符串文字。 在 Python 2 中,b() 返回一个 8 位字符串。在 Python 3 中,数据被编码 将 latin-1 编码为字节。
所以它看起来像这样:
from __future__ import unicode_literals
import six
name='FooClass'
type(six.b(name), (dict,), {})
但它在 python2 和 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 字符串。
你怎么看?
有什么我不明白的地方吗?
或者在 python 2 和 3 上与 type() 是否存在真正的不兼容?
难道没有任何方法可以让相同的type() 呼叫同时支持 2 和 3 吗?
在这种情况下,像 six 这样的工具不应该为 type() 提供一个包装器吗?
【问题讨论】:
-
谢谢,这行得通,但我确信我已经测试过了......
标签: python python-3.x python-2.x six-python