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 Dates and Times in Python (2014) Dates and Times Manipulating Time Already

Ting-Chieh Huang
Ting-Chieh Huang
2,760 Points

How datetime.datetime.now() work? Please help.

I remember Kenneth said that if we don't create an instance of class then we cannot directly call its method. Then why we can call "now" method in datetime class in datetime library?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

There are two types of methods in a class: class method and instance method. The instance method is the one you've mentioned and is only available after an instance of the class has been created. This is because the method manipulates or operation on a specific instance of the class.

The class method is available directly from the class and does not need an instance to operate since it does not refer to any specific instance.

datetime.datetime.now() is a class method (doc) so it needs no instance.