2

I've got a string which will form a timedelta object based on its value. I could write this with if statements, but I know that the string will always match one of the named arguments for the timedelta object. I want to do something similar to below:

td = timedelta("hours"=5)

or:

my_var = "days"
td = timedelta(my_var=5)

What would be the recommended way to do this?

0

1 Answer 1

4

Use the double-asterisk kwargs syntax:

td = timedelta(**{my_var: 5})
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.