Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Welby Obeng
20,340 Pointsa little confused about python library, class, method
import datetime # This means I'm importing the datetime library correct?
print datetime.datetime.now() # Is the first datetime referring to the library again or class? what is the second datetime? Is now() the method?
print datetime.datetime.now().strftime("%y-%m-%d-%H-%M") # Does datetime.datetime.now() return an object? how can you get strftime method from it if when I print it, it's returning 2012-10-03 15:35:46.461491?
1 Answer

Mario Blokland
19,750 PointsHi Welby Obeng,
import datetime # This means I'm importing the datetime library correct?
correct.
print datetime.datetime.now() # Is the first datetime referring to the library again or class?
# what is the second datetime? Is now() the method?
The first datetime
is referring to the module. The second one is referring to the datetime
class and the method now
is inside the class datetime
, which is in the module datetime
.
print datetime.datetime.now().strftime("%y-%m-%d-%H-%M")
# Does datetime.datetime.now() return an object?
# how can you get strftime method from it if when I print it, it's returning 2012-10-03 15:35:46.461491?
Yes, the now()
method is returning a datetime
object and with the strftime()
method, you are declaring how the output of the date object should look like.
It will print the time from your machine on which you run the script on.
Hope I could help. Please let me know if you have any further questions.
Welby Obeng
20,340 PointsWelby Obeng
20,340 Pointsso when I print datetime.datetime.now does it print str of the object?
Mario Blokland
19,750 PointsMario Blokland
19,750 PointsCorrect Welby,
it outputs the
datetime
object as astring
. Theprint()
function uses thestr()
function and converts the datetime object to a string.