Question on sort() key function

R

Robert Latest

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
 
P

Paul Rubin

Robert Latest said:
flist.sort(key=File.mod_date.toordinal)

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

The attribute is on instances of File, not on the class itself. See
if this works:

flist.sort(key=lambda f: f.mod_date.toordinal)
 
R

Robert Latest

Paul said:
The attribute is on instances of File, not on the class itself. See
if this works:

flist.sort(key=lambda f: f.mod_date.toordinal)

It doesn't throw an error any more, but neither does it sort the list. This,
however, works:
 
P

Peter Otten

Robert said:
It doesn't throw an error any more, but neither does it sort the list. This,
however, works:

----------------------
def by_date(f1, f2):
return f1.mod_date.toordinal() - f2.mod_date.toordinal()

flist.sort(by_date)
----------------------

So I'm sticking with it, although I sort of liked the key approach.

robert

This should work then:

def date_key(f):
return f.mod_date.toordinal()
flist.sort(key=date_key)

This can also be written as

flist.sort(key=lambda f: f.mod_date.toordinal())

Peter
 
R

Robert Latest

Peter said:
This should work then:

def date_key(f):
return f.mod_date.toordinal()
flist.sort(key=date_key)

This can also be written as

flist.sort(key=lambda f: f.mod_date.toordinal())

Well, that's almost Paul's (non-working) suggestion above, but it works
because of the parentheses after toordinal. Beats me how both versions can
be valid, anyway.

To me it's all greek. I grew up with C function pointers, and they
always work.

robert
 
M

Marc 'BlackJack' Rintsch

Well, that's almost Paul's (non-working) suggestion above, but it works
because of the parentheses after toordinal. Beats me how both versions can
be valid, anyway.

To me it's all greek. I grew up with C function pointers, and they
always work.

robert

Suppose `func` is a C function pointer, then

foo = func;

and

foo = func();

have different meanings. It's just the same in Python. First is the
function itself, second *calls* the function.

Ciao,
Marc 'BlackJack' Rintsch
 
P

Paul Rubin

Robert Latest said:
It doesn't throw an error any more, but neither does it sort the list. This,
however, works:

Oh, I didn't realize that toordinal was a callable, given your earlier
sample. You want:

flist.sort(key=lambda f: f.mod_date.toordinal() )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top