Home > Back-end >  How can i change a function argument depending on the type of variable?
How can i change a function argument depending on the type of variable?

Time:12-17

I have this piece of code i'm using for a telegram bot:

r = requests.post("https://api.telegram.org/botMY_BOT_ID/sendPhoto?chat_id=MY_CHAT_ID",files=stuff)
print(r.status_code)
print(r.content)

Theres any way to change the "files=stuff" to "data=stuff" depending on the type of "stuff"(bytes or string), without writing another requests line ?

CodePudding user response:

Use a dict display that evaluates an expression to generate the correct keyword.

requests.post(..., **{kind_of_stuff(stuff): stuff})

where kind_of_stuff is a function that returns 'file' or 'data' as appropriate based on the type of stuff.

CodePudding user response:

Maybe something like this?

r = requests.post("...", **{"file":stuff} if //condition else {"data": stuff})
  • Related