[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Question on sort() key function

Robert Latest

1/22/2008 8:46:00 AM

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
6 Answers

Paul Rubin

1/22/2008 8:50:00 AM

0

Robert Latest <boblatest@yahoo.com> writes:
> 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)

Robert Latest

1/22/2008 9:12:00 AM

0

Paul Rubin wrote:
> 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:

----------------------
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

Peter Otten

1/22/2008 9:30:00 AM

0

Robert Latest wrote:

> Paul Rubin wrote:
>> 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:
>
> ----------------------
> 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

Robert Latest

1/22/2008 9:57:00 AM

0

Peter Otten wrote:
> Robert Latest wrote:
>
>> Paul Rubin wrote:
>>> 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:
>>
>> ----------------------
>> 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())

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

Marc 'BlackJack' Rintsch

1/22/2008 10:05:00 AM

0

On Tue, 22 Jan 2008 09:56:55 +0000, Robert Latest wrote:

> Peter Otten wrote:
>> Robert Latest wrote:
>>
>> 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

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

Paul Rubin

1/22/2008 1:25:00 PM

0

Robert Latest <boblatest@yahoo.com> writes:
> > 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:

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() )