0

Is it possible to convert string to method's argument?

Let say I can get such strings 'weeks', 'months', 'years'.

Now I have this:

from dateutil.relativedelta import relativedelta
from datetime import date

today = date.today()

So after this I need to increment today by the type of duration that is written in string. For example if string would be provided 'years', then it should do this (duration number let say is 10):

some_date = today + relativedelta(years=10)

Of course I could do this using ifs and checking what kind of string was passed. But maybe there is better way to just convert string to that keyword, so I would not need many ifs for this approach?

2
  • where are "years" and 10 coming from? Commented Feb 23, 2015 at 10:52
  • @PadraicCunningham does it matter where it is coming from? Commented Feb 24, 2015 at 8:03

1 Answer 1

4

You can make use of the keyword argument feature of python

some_date = today + relativedelta(**{'years': 10})

You can also prepare the dictionary before use

kwargs = {}
kwargs['years'] = 10
some_date = today + relativedelta(**kwargs)

For more information:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.