相关疑难解决方法(0)

有类的字典?

在Python中是否可以通过字典实例化一个类?

shapes = {'1':Square(), '2':Circle(), '3':Triangle()}

x = shapes[raw_input()]
Run Code Online (Sandbox Code Playgroud)

我想让用户从菜单中选择,而不是在输入上编写大量if else语句.例如,如果用户输入2,则x将是Circle的新实例.这可能吗?

python dictionary class

8
推荐指数
1
解决办法
7819
查看次数

标签 统计

class ×1

dictionary ×1

python ×1