您如何以编程方式将方法添加到类调用中?

时间:2018-12-31 16:37:57

标签: python python-3.x

我使用Python已有很长时间了,所以如果我弄错了术语,请教育我。

我有2个变量存储为文本,第一个是 Class 名称,第二个是 method 名称。我需要从中调用并返回数据。要手动执行此操作:

    my_send = Resource()
    my_send.requires = True
    my_send.id = 1

    my_return = MyTest(end_point=my_send.details()).make_call()

设置 Class 很简单:

    my_send = dapi.__dict__[self.resource]()

参数也是如此。

    for each in self.__dict__:
        setattr(my_send, each, self.__dict__[each])

我无法做的是添加方法,就像我得到的解决方案一样:

    <bound method User.Subscribe of <d.api.User object at 0x7f87c2157e10>>

我需要学习如何使用绑定方法或如何以正确调用 my_send 的方式添加方法 ,而且许多小时的阅读都没有结果。

1 个答案:

答案 0 :(得分:1)

解决方案是:

    type(my_send).__dict__[self.end_point].__get__(my_send, type(my_send))()
相关问题