4

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.

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
3db
  • 51
  • 4
  • 1
    I am unsure how the second snippet of code is related to the first snippet (or the third one). What is `dapi`? What is the `self` in the third snippet? Sometimes it's more useful to outline _what_ you are trying to do, than _how_ you tried to do it. – Jan Christoph Terasa Dec 31 '18 at 16:42
  • https://stackoverflow.com/a/17930262/7919597 – Joe Dec 31 '18 at 17:02
  • https://stackoverflow.com/questions/8276733/dynamically-add-methods-to-a-class-in-python-3-0 – Joe Dec 31 '18 at 17:03
  • https://medium.com/@mgarod/dynamically-add-a-method-to-a-class-in-python-c49204b85bd6 – Joe Dec 31 '18 at 17:03
  • That is the outline, I don't have this copyrighted yet so I am not going into a great deal of specifies. I should have left off the first example. – 3db Dec 31 '18 at 17:26
  • Maybe this will make it a clear to all of us, I'm still learning this to. A selection process yields 2 variables, these variables need to invoke a Class.method(). We can leave out the other stuff for now because it's a general question. – 3db Dec 31 '18 at 17:41
  • I know how to add a method to the Class, so unless the solution is to create a temporary method I don't see how that will help. If the call is made like this _class.method()_ it works. The issues is I don't know what _method_ is before hand. I don't want to write 115 of those either. If the call is made like this _Class.method_ it's returns gives me the __bound method__. So is there a way to access that bound method. I assume since there is a method that it contains the results I need. – 3db Dec 31 '18 at 17:46
  • Thanks Joe your response lead me to https://stackoverflow.com/questions/972/adding-a-method-to-an-existing-object-instance which I think is the answer. – 3db Dec 31 '18 at 17:52
  • The solution is `type(my_send).__dict__[self.end_point].__get__(my_send, type(my_send))()` – 3db Jan 01 '19 at 05:55

1 Answers1

1

The solution is:

    type(my_send).__dict__[self.end_point].__get__(my_send, type(my_send))()
Tom Zych
  • 13,329
  • 9
  • 36
  • 53
3db
  • 51
  • 4