Create datetime instance using a tuple.

N

Negroup

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: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]]]]])

Thanks.
 
Q

Qiangning Hong

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: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)
 
D

Dan Bishop

Qiangning said:
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.
 
N

Negroup

Use:
dt =3D datetime(*t)

Thanks for the quick reply.
I can't find any doc about '*' used in this context. Have you some url
or suggestion for which terms search in Google?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top