3db
3db

Reputation: 51

How do you add a method programmatically to a class call?

I have not been working with Python for very long, so if I get the terminology wrong, please educate me.

I have 2 variables stored as text, the first is the Class name, the second is the method name. I need to invoke and return data from it. To do this manually:

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

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

Setting the Class was easy as pie:

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

So are the arguments.

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

What I have not been able to do is add the method, as near to a solution as I have gotten is:

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

I either need to learn how to use the bound method or how to add the method to my_send in a way that correctly invokes it, and many hours of reading has not yielded a result.

Upvotes: 4

Views: 115

Answers (1)

3db
3db

Reputation: 51

The solution is:

    type(my_send).__dict__[self.end_point].__get__(my_send, type(my_send))()

Upvotes: 1

Related Questions