如何使用字典创建动态类名?

2024-04-20 09:44:55 发布

您现在位置:Python中文网/ 问答频道 /正文

在python中使用dictionary可以创建动态类名吗

a={'a':'hai','b','hello'}

我希望类名是

class hai():
  class content
class hello():
  class content 

Tags: hellodictionary动态contentclass类名hai
1条回答
网友
1楼 · 发布于 2024-04-20 09:44:55

通常使用^{}创建全局变量不是一个好主意,但是这里使用^{}^{}的3参数版本。你知道吗

>>> a = {'a':'hai','b': 'hello'}
>>> for v in a.values():
    globals()[v] = type(v, (), {})


>>> hai
<class '__main__.hai'>
>>> hello
<class '__main__.hello'>

相关问题 更多 >