普通网友 2022-12-13 11:55 采纳率: 26.3%
浏览 9
已结题

Is there a way to call specific classes based on a variable?

New
I want a program to call a specific class based on a parameter/variable value. However, I don't want to use any clunky if-statements. My first thought was to use the globals() function, but I couldn't get it to work. He's an example:


class SomeClass:
    def __init__():
        print("Hello, world!")

class OtherClass:
    def runClass(className):
        # Call class based on variable className

The reason I want to do this is because there is a wide variety of classes may need to be called, and so just piling up if-statements in my code won't do it. Any help would be greatly appreciated. Thanks!

  • 写回答

1条回答 默认 最新

  • 普通网友 博客专家认证 2022-12-13 12:05
    关注

    已经知道答案啦

    评论

报告相同问题?

问题事件

  • 系统已结题 12月21日
  • 修改了问题 12月13日
  • 修改了问题 12月13日
  • 修改了问题 12月13日
  • 展开全部

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题