Skip to main content
 首页 » 编程设计

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

2024年11月01日24oomusou

翻译以下内容为 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, ), {}) 

但我很确定这没有意义。哪种处理方式才是正确的?

请您参考如下方法:

查看 the Python Bilingual QuickRef .看起来你做的是对的,他们也使用 str('ClassName') 结构。