Question on sort() key function

Discussion in 'Python' started by Robert Latest, Jan 22, 2008.

  1. 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
    Robert Latest, Jan 22, 2008
    #1
    1. Advertising

  2. Robert Latest

    Paul Rubin Guest

    Robert Latest <> 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)
    Paul Rubin, Jan 22, 2008
    #2
    1. Advertising

  3. 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
    Robert Latest, Jan 22, 2008
    #3
  4. Robert Latest

    Peter Otten Guest

    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
    Peter Otten, Jan 22, 2008
    #4
  5. 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
    Robert Latest, Jan 22, 2008
    #5
  6. 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
    Marc 'BlackJack' Rintsch, Jan 22, 2008
    #6
  7. Robert Latest

    Paul Rubin Guest

    Robert Latest <> 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() )
    Paul Rubin, Jan 22, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    9,259
    Martin Honnen
    Sep 5, 2006
  2. Replies:
    7
    Views:
    739
    Stefan Arentz
    Sep 10, 2007
  3. sajuptpm
    Replies:
    7
    Views:
    806
    Terry Reedy
    Sep 8, 2010
  4. Navin
    Replies:
    1
    Views:
    685
    Ken Schaefer
    Sep 9, 2003
  5. Antonio Quinonez
    Replies:
    2
    Views:
    167
    Antonio Quinonez
    Aug 14, 2003
Loading...

Share This Page