Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Andy Hughes8,114 Points
Question - where does attribute 'day' come from?
Just a question to help my confusion. In this code challenge I used the following code line, which passed the challenge.
sorted_dates = sorted(date_list, key=attrgetter('day'))
But originally I used this, which failed:
sorted_dates = sorted(date_list, key=attrgetter('%d'))
My question is; where does 'day' come from as an attribute? It's not explicitly mentioned in the code anywhere. So, more by experimenting, I put the word 'day' in.
Second question :P; So how would I ordinarily know that the attribute is called 'day'. Is it from the docs? Did I miss it in an older video? (ok maybe it wasn't just one question lol).
It's confused me though and I don't like staying confused! :)
import datetime from operator import attrgetter date_list = [ datetime.datetime(2015, 4, 29, 10, 15, 39), datetime.datetime(2006, 8, 15, 14, 59, 2), datetime.datetime(1981, 5, 16, 2, 10, 42), datetime.datetime(2012, 8, 9, 14, 59, 2), ] sorted_dates = sorted(date_list, key=attrgetter('day'))