Question on sort() key function

Robert Latest boblatest at yahoo.com
Tue Jan 22 03:45:36 EST 2008


Hello,

I have this class:

class File:
	def __init__(self):
		self.name = ''
		self.path = ''
		self.date = 0
		self.mod_date = 0
		self.keywords = []
		self.url = ''


...and after creating a list of File objects called flist, I'd like to sort 
it like thus:

flist.sort(key=File.mod_date.toordinal)

However, Python says:
AttributeError: class File has no attribute 'mod_date'

Well if you ask me, there are many things that may be said about my File 
class, but the absence of the attribute 'mod_date' ain't one of them. What 
do you think?

And yes, this loop works fine:

for f in flist:
	print f.mod_date.isoformat()

(which IMO proves that all mod_date members are properly initialized as 
datetime objects).

robert



More information about the Python-list mailing list