Ugn*_*kas 3 python dictionary python-2.7 python-3.x
我试图通过获取dict元素新对象来设置每个字典元素的类定义.
例:
types = {}
types[first_type] = FirstType()
types[second_type] = SecondType()
Run Code Online (Sandbox Code Playgroud)
通过设置types[first_type]我必须得到new FirstType():
some_var = new types[first_type] # this is illegal statement.
Run Code Online (Sandbox Code Playgroud)
我怎样才能在Python中实现这一目标?
尝试将它们自己变成"对象生成器".
types = {}
types[first_type] = FirstType
types[second_type] = SecondType
Run Code Online (Sandbox Code Playgroud)
然后你可以通过调用那个点来每次获得一个新对象.
some_var = types[first_type]()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71 次 |
| 最近记录: |