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?

famousgarkin
  • 13,687
  • 5
  • 58
  • 74
joeButler
  • 1,643
  • 1
  • 20
  • 41

1 Answers1

4

Use the double-asterisk kwargs syntax:

td = timedelta(**{my_var: 5})
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895