使用内置
getattr()功能:
class Foo: def bar1(self): print(1) def bar2(self): print(2)def call_method(o, name): return getattr(o, name)()f = Foo()call_method(f, "bar1") # prints 1
您还可以
setattr()用于通过名称设置类属性。

使用内置
getattr()功能:
class Foo: def bar1(self): print(1) def bar2(self): print(2)def call_method(o, name): return getattr(o, name)()f = Foo()call_method(f, "bar1") # prints 1
您还可以
setattr()用于通过名称设置类属性。