我知道如何传递函数作为参数,然后稍后使用该函数。但是我不知道如何用方法来做到这一点。我想为用户提供一个“API”,允许他们配置自定义功能。
作为一个(非)工作示例,假设我希望用户能够提供一些通用的 pandas 函数:
import pandas as pd
# user-supplied data
right = pd.DataFrame({'right_a':[1,2],
'right_b':[3,4]})
# user-supplied function & arguments
myfun = pd.DataFrame.merge
args = {'right':right,
'right_on':'right_a',
'how':'inner'
}
user_configs = {'func': myfun,
'args':args}
def exec_func(exec_dict):
# some data that only the function knows:
left = pd.DataFrame({'left_a':[1,2],'left_b':['foo','bar']})
# some args that only the function knows
exec_dict['args']['left_on'] = 'left_a'
# apply the function
#result = left.merge(**exec_dict['args']) # desired result
result = left.exec_dict['func'](**exec_dict['args']) # generalized but not working code
return result
exec_func(user_configs)
上面的代码结果是
AttributeError: 'DataFrame' object has no attribute 'exec_dict'
出于显而易见的原因。我怎样才能实现期望的行为,允许用户提供不同的功能?
最佳答案
所以我稍微简化了你的问题并省略了 pandas。通过名称调用实例方法的方法是通过 getattribute 方法:
class MethodProvider (object):
def __init__(self):
pass
def method1(self, foo):
print('Method1 was called with arguments %s'%foo)
def method2(self, baz):
print('Method2 was called with argument %s'%baz)
def exec_method(obj, meth, kwargs):
func = obj.__getattribute__(meth)
func(**kwargs)
# Test construction
mp = MethodProvider()
exec_method(
mp,
'method1',
{'foo': 'bar'}
)
exec_method(
mp,
'method2',
{'baz': 'buzz'}
)
关于python:将实例方法及其参数传递给函数以执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64681829/