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! While you're at it, check out some resources Treehouse students have shared here.

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

Python

Welby Obeng
Welby Obeng
20,340 Points

a 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

Hi 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
Welby Obeng
20,340 Points

so when I print datetime.datetime.now does it print str of the object?

Correct Welby,

it outputs the datetime object as a string. The print() function uses the str() function and converts the datetime object to a string.