如何根据班级名称获得现有类?
我有一个类:
class Test():
pass
我知道类名称为“ test”
。如何获得类测试
?课程是课堂的对象。我想根据其名称,文本“ test”
获得类对象。
在我的项目中,我有许多定义的课程。我想根据该类的名称实例化(如果不使用语句)。
I have a class:
class Test():
pass
I know the class name is "Test"
. How can I get class Test
? A class is an object of class. I would like to get the class object based on its name, the text "Test"
.
In my project I have many classes defined. I would like to instantiate a class based on its name (without using an if
statement).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果类是在全局名称空间中定义的,则可以这样做:
If the class is defined in the global namespace, you can do it this way:
我不建议遵循这样的惯例,因为这是非常糟糕的做法。类不是类的对象,而是已经定义的类。您使用该类定义的任何对象都将是该类的对象,并且具有自己的实例。请不要在不同的类中使用相同的名称,这几乎永远无法维护,您永远都不应该这样做。
I don't suggest following such a convention, as this is very bad practice. A class is not an object of class, it's just a class that has been defined. Any objects you define using that class will be an object of that class and have its own instance. Please don't use the same name for different classes, this is almost never maintainable and you should never do it this way.