#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Question about modules


    Why do I need "from datetime import datetime" in the following code?

    import math
    import random
    import time
    from datetime import datetime

    def first():
    hour = datetime.time(datetime.now()).hour
    minute = datetime.time(datetime.now()).minute
    second = datetime.time(datetime.now()).second
    milisecond = datetime.time(datetime.now()).microsecond/1000
    return hour, minute, second, milisecond

    I know it won't work without it, but why is that? I tried with only import datetime, it didn't work.
    Thanks for the reply in advance
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    This module looks screwy, but I didn't read the manual. The datetime you imported is a class in the datetime module. Why a constructor for objects of class datetime class is hidden in a method of the class I cannot say.
    Code:
    import math
    import random
    import time
    import datetime
    
    def first():
        o = datetime.datetime.today() # construct a datetime instance of now
        return o.hour,o.minute,o.second,o.microsecond/1000
    
    print(first())
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo