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 strftime & strptime

Kohei Ashida
Kohei Ashida
4,882 Points

the difference between to_string and from_string functions.

I solved this challenge with following lines of code but I couldn't get why to_string function works with "return my_datetime.strftime('%d %B %Y')" not "return datetime.datetime.strftime(my_datetime, '%d %B %Y')" like as from_string function does.

Is this because strftime is an instance method and strptime is a class method?

import datetime

def to_string(my_datetime):
    return my_datetime.strftime('%d %B %Y') # Shoudn't it be like "datetime.datetime.strftime(my_datetime, '%d %B %Y')"?

def from_string(my_date, my_format):
    return datetime.datetime.strptime(my_date, my_format)

1 Answer

Steven Parker
Steven Parker
231,269 Points

You're exactly right, an instance method has access to the instance it is called on, and doesn't need to have it passed as an argument.

Kohei Ashida
Kohei Ashida
4,882 Points

Thanks a lot! it cleared up my eyes!