On Jul 8, 2005, at 6:57 AM, Mario Ruggier wrote: > On Jul 8, 2005, at 12:31 PM, David Binger wrote: > > >> If the other is an instance of Persistent with no _id >> attribute, I think this should really raise an exception. >> > > I have modified it again since... > > def __cmp__(self, other): > if self is other: > return 0 > try: > return cmp( (self._container.__class__.__name__, > self._id), > (other._container.__class__.__name__, > other._id) ) > except AttributeError: > return cmp(self._id, other) > > Because, the id can be the same for different items in different > containers. Be aware that this means that a change of class name could then hose your BTree. > > I am not really worried so much about raising the AttributeError... > but I am not sure in what contexts a p_item will be compared to > arbitrarily some other kind of object. It will certainly never > happen during the comparison of btree keys (in moellus)... I just > lazily threw in that last-ditch cmp, to not have to deal with it > properly, for now (and, as i said, i think this situation should > not be pertinent to my application). I'd just leave it out if I were you. > > Do you think that replacing the last-ditch comparison with: > > return cmp(self._id, str(other)) > > is a better idea? It would be stable at least... If you do this, then you could conceivably accidentally put something in your BTree that is not what you expected. The str of that accidental key might be different in a different process, or because someone changes it later. One might reasonably expect such a change to be safe, but it would break your BTree. For a class used as a key for anything, I think the __cmp__ should provide a reliable, stable, total ordering for the entire "domain". For any "other" outside the domain, it should raise an exception. the reliable-stable-total ordering requirement should apply to the entire "domain" of the __cmp__.