Create datetime instance using a tuple.

Dan Bishop danb_83 at yahoo.com
Wed Jul 6 11:36:49 EDT 2005


Qiangning Hong wrote:
> On 6 Jul 2005 02:01:55 -0700, Negroup <negroup at gmail.com> wrote:
> > Hi, all.
> > I would like to know if it is possible to create a datetime instance
> > using a tuple instead of single values.
> >
> > I mean:
> > >>> from datetime import datetime
> > >>> t = (1, 2, 3)
> > >>> dt = datetime(t)
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > TypeError: function takes at least 3 arguments (1 given)
> >
> > (class datetime(year, month, day[, hour[, minute[, second[,
> > microsecond[, tzinfo]]]]])
>
> Use:
> dt = datetime(*t)

It's better to write:

dt = datetime(*t[:6])

This gives you compatibility with the (year, month, day, hour, minute,
second, weekday, julian_day, dst) tuples returned by time.gmtime,
time.localtime, and time.strptime.




More information about the Python-list mailing list