2名名名名名名名名名名名名名名名名名名名 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条回答 默认 最新

  • 2名名名名名名名名名名名名名名名名名名名 领域专家: 后端开发技术领域 2022-12-13 12:05
    关注

    已经知道答案啦

    评论

报告相同问题?

问题事件

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