Python 2中的元类创建字符串名称支持3

时间:2014-04-09 14:48:21

标签: python python-2.7 python-3.x

将以下内容翻译成Python 3的正确方法是什么?

type('Name', (object, ), {})

原则上这条线保持不变,但是当我使用Django时,我开始在我的所有文件中添加以下行,如here所述:

from __future__ import unicode_literals 

所以我有

from __future__ import unicode_literals 
type('Name', (object, ), {})

反过来给出了

TypeError: type() argument 1 must be string, not unicode

以下作品

from __future__ import unicode_literals 
type(str('Name'), (object, ), {})

但我很确定它没有意义。处理它的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

结帐 the Python Bilingual QuickRef 。看来你做的是正确的,他们也使用str('ClassName')构造。