gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:47:35 24 4
gpt4 key购买 nike

翻译以下内容为 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') 结构。

关于python - Python 2 支持 3 中的元类创建字符串名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22966213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com