str as param for timedelta

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


Philipp wrote:
> from datetime import *
>
> def f(gap):
> 	print (datetime.now() + 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 datetime.now() + timedelta(**kwargs)

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

http://docs.python.org/tut/node6.html#SECTION006720000000000000000




More information about the Python-list mailing list