str as param for timedelta

dylan.moreland at dylan.moreland at
Fri Jan 27 06:27:41 EST 2006

Philipp wrote:
> from datetime import *
> def f(gap):
> 	print ( + datetime.timedelta(gap))
>   f('hours = -8')

When you need a flexible input format, consider keyword arguments. In
this case, I would pass along the inputs to timedelta:

def from_now(**kwargs):
    return + timedelta(**kwargs)

from_now(hours=8) returns + timedelta(hours=8), and the
function can now accept any of timedelta's arguments. For more on what
**kwargs means, see the tutorial:

More information about the Python-list mailing list