Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
sname = "example" some_class.<sname>.do_stuff()
sname是 中的子类的名称some_class。是否可以在我的调用中动态引用字符串,如此some_class.example.do_stuff()调用?
sname
some_class
some_class.example.do_stuff()
您正在寻找getattr()内置:
getattr()
getattr(some_class, sname).do_stuff()