Where do we create an instance of class datetime?



My code doesn't exactly behave incorrectly, I'm just seeking clarification. I don't quite get how the datetime class works. We apparently call the now() method of datetime without creating an object. Also what happens after that when we print now.year etc.? Those don't even look like functions.

`from datetime import datetime

now = datetime.now()
print now

print now.year
print now.month
print now.day`


you import the datetime object, agree? the now method (a method is a function in a object) is part of the datetime object you imported.

they are not methods indeed, they are attribute references:

class Test(object):
    aProperty = "see"
variable = Test()

print variable.aProperty

I created a very simple object, with a attribute references. see how use the . reference name to get the attribute references? You can read more about it here


Oh ok. So what I imported actually already was an object and not simply the class?

If that is the case and you can simply refer to attributes with a dot then I understand what's going on, Thanks.


i have to be more careful with my terminology.

You imported a class (datetime) and here:

now = datetime.now()

you created the object now which is a instance of your class. see here


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.