如果在课程中,则可以使用getattr:
class MyClass(object): def install(self): print "In install"method_name = 'install' # set by the command line optionsmy_cls = MyClass()method = Nonetry: method = getattr(my_cls, method_name)except AttributeError: raise NotImplementedError("Class `{}` does not implement `{}`".format(my_cls.__class__.__name__, method_name))method()
或者它是一个函数:
def install(): print "In install"method_name = 'install' # set by the command line optionspossibles = globals().copy()possibles.update(locals())method = possibles.get(method_name)if not method: raise NotImplementedError("Method %s not implemented" % method_name)method()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)