how to initialize time() object in python how to initialize time() object in python python python

how to initialize time() object in python


You can create the object without any values:

>>> import datetime>>> datetime.time()datetime.time(0, 0)

You, however, imported the class datetime from the module, replacing the module itself:

>>> from datetime import datetime>>> datetime.time<method 'time' of 'datetime.datetime' objects>

and that has a different signature:

>>> datetime.time()Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: descriptor 'time' of 'datetime.datetime' object needs an argument>>> datetime.time(0)Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: descriptor 'time' requires a 'datetime.datetime' object but received a 'int'

Either import the whole module, or import the contained classes, but don't mix and match. Stick to:

import datetimeimport time

if you need both modules.


The constructor for time is:

class datetime.time(hour[, minute[, second[, microsecond[, tzinfo]]]])

(from http://docs.python.org/library/datetime.html#time-objects)

This works for me:

In [1]: import datetimeIn [2]: t = datetime.time(0, 0, 0)In [3]: print t00:00:00


It's the fact that you're importing a conflicting datetime from datetime. You probably meant time, except you're also importing a conflicting time. So how about:

import datetime as dt

and

t = dt.time(0, 0, 0)