RE: ExtensionClass/Persistent and __cmp__ is tricky

Discussion in 'Python' started by Tim Peters, Jun 29, 2003.

  1. Tim Peters

    Tim Peters Guest

    I want to cut to the chase here (I really don't have time for more now --
    sorry!):

    [Tim]
    >> It also doesn't make much sense to compare a Foo against an integer.


    [Christian Reis]
    > Maybe not, but it does work for `normal' instances. Should I raise a
    > TypeError instead in my __cmp__() definition?


    If I understand what you're after, you're trying to use persistent objects
    as keys in BTree-based data structures. If so, one of the other rules at

    http://www.zope.org/Wikis/ZODB/guide/node6.html

    is

    Within a single btree-based data structure, use objects of a single
    type as keys. Don't use multiple key types in a single structure.

    If you want to make life predictable, stick to that rule, and then yes,
    since you then *intend* that your BTree have keys of a single type, you'd be
    doing yourself a huge favor by raising an exception whenever it's possible
    to do so during a mixed-type comparison.

    Jim took a stab at explaining what he thought was going on during mixed-type
    comparisons involving ExtensionClass objects, and that explanation didn't
    match all the behaviors you saw either. So just take it as a fact that
    nobody understands all the behaviors you're stumbling into when playing with
    mixed-type comparisons, and avoid them instead.

    > I'm unsure of the `correct' semantics here, more than anything.


    Then settle for useful semantics <wink>.

    > ...
    > I expected consistency when comparing with basic types; sorry if I
    > wasn't clear enough. However, if the correct policy is to raise a
    > TypeError, that's fine (and simpler) with me.


    For your app, those appear to be the useful semantics.

    > ...
    > It's showing to be a bit embarassing but highly enlightening
    > (as all newbie things are).


    To the contrary, how mixed-type comparisons work in all cases when an
    ExtensionClass is involved is beyond wizards at this time. I'll claim
    without proof that how mixed-type comparisons work with Python classes is
    also beyond wizard prediction in all cases: the implementation of
    comparison in Python is crushingly complicated. Life here is easier in
    Python 2.3 -- or, it would be, if 2.3 weren't also striving to be compatible
    with the mass of pre-2.3 comparison rules.
    Tim Peters, Jun 29, 2003
    #1
    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. Jim Fulton
    Replies:
    0
    Views:
    666
    Jim Fulton
    Jun 29, 2003
  2. Lee Harr

    custom sorting and __cmp__

    Lee Harr, Nov 30, 2003, in forum: Python
    Replies:
    3
    Views:
    466
  3. Alex

    dict and __cmp__() question

    Alex, Sep 7, 2005, in forum: Python
    Replies:
    2
    Views:
    273
    Bryan Olson
    Sep 7, 2005
  4. Replies:
    9
    Views:
    534
    CBFalconer
    Apr 25, 2006
  5. gk
    Replies:
    7
    Views:
    969
    Tom Anderson
    Oct 12, 2010
Loading...

Share This Page