0

I have a Sage class that inherits from SageObject. According to the Python documentation,

User-defined classes have __cmp__()and __hash__() methods by default; with them, all objects compare unequal (except with themselves) and x.__hash__() returns id(x).

However, my class doesn't do this, despite the fact that it doesn't implement a __hash__ method of its own. Instead, it uses the hash value of its string representation (the one returned by its __str__ method). Is this part of the design of Sage classes, something different from normal Python classes? Is there a hierarchy of places that Sage might look in order to find an acceptable hash value?

Scott M
  • 115
  • 6

1 Answers1

1

Luckily, you practically answered the question yourself. Try the ?? trick to find the source code.

sage: SageObject.__hash__??
Type:       wrapper_descriptor
Base Class: <type 'wrapper_descriptor'>
String Form:    <slot wrapper '__hash__' of 'sage.structure.sage_object.SageObject' objects>
Namespace:  Interactive
Definition: SageObject.__hash__(self)
Source:
    def __hash__(self):
        return hash(self.__repr__())

So yes, it's intentional for most of these things. If you wanted to implement something different for hashes, I guess you could. It would be worth asking on one of the Sage lists if this was code you were interested in contributing and thought it might conflict with something.

kcrisman
  • 4,374
  • 20
  • 41