0

How I can call class method via variable name, example:

def http_call(http_method):
 url = "https://httpbin.org/get"
 response = requests.http_method(url)
 return response

http_call("get")
http_call("post")

Thanks beforehand.

dmitryro
  • 3,405
  • 2
  • 20
  • 27
azatuni
  • 43
  • 6
  • 2
    I don't see any class in your code. – Klaus D. Oct 24 '20 at 19:51
  • 1
    @KlausD. I believe they want to change what `requests` function is called based on the passed-in string. – Carcigenicate Oct 24 '20 at 19:52
  • This question already has answer https://stackoverflow.com/questions/3521715/call-a-python-method-by-name – dmitryro Oct 24 '20 at 19:55
  • 2
    Does this answer your question? [Call a Python method by name](https://stackoverflow.com/questions/3521715/call-a-python-method-by-name) – Ironkey Oct 24 '20 at 19:56
  • 1
    Although, for this particular case, you might as just use `requests.request(http_method, url)`. `request` is what `.get` and such use behind the scenes anyways. – Carcigenicate Oct 24 '20 at 19:57
  • @Carcigenicate cause I am using different http methods of requests library and for example in case of post request it will be `requests.post(url)` – azatuni Oct 24 '20 at 20:19
  • @azatuni Yes, as I mentioned, you can use `requests.request` instead. It takes a string as is first argument. – Carcigenicate Oct 24 '20 at 20:21

0 Answers0