[Pyrex] __richcmp__ , __hash__: dependencies on subclassing ?
Uwe Hoffmann
qual at tiscali.de
Sun Jan 29 16:45:21 CET 2006
Hi,
i have two pyrex extension types Base and Sub where Sub is
derived from Base. Base has a __richcmp__ method but not a
__hash__ method, Sub has a __hash__ method. Now if i compare
instances of Sub (a==b) it seem that this is actually a test
for object identity (__richcmp__ is *not* called) and the result
is always False if id(a) != id(b) and True if id(a) == id(b).
On removing the __hash__ method of the Sub class everything is
fine (__richcmp__ of class B is called).
If i build the same class hierachy within native python this
behaviour is not observed (__eq__ of the Base class is always
called)
Any hints ?
regards
Uwe
hoping that the indentation is correctly displayed
#missing.pyx
cdef class Base:
cdef int a
def __new__(self,*args,**kw):
pass
def __init__(self,*args,**kw):
print args[0]
self.a=args[0]
def __richcmp__(self,other,case):
print "__richcmp__"
if case == 2:
return bool((<Base>left).a == (<Base>right).a)
cdef class Sub(Base):
def __hash__(self):
print "__hash__"
return hash(self.a)
Python 2.4.2 (#2, Sep 30 2005, 22:19:27)
[GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from missing import Base,Sub
>>> a=Base(1)
1
>>> aa=Base(1)
1
>>> b=Base(2)
2
>>>
>>> sa=Sub(1)
1
>>> saa=Sub(1)
1
>>> sb=Sub(2)
2
>>> print a==aa
__richcmp__
True
>>> print a==b
__richcmp__
False
>>>
>>> print sa==saa # huh ?!?! <---------
False
>>> print sa==sb
False
>>> print sa==sa
True
>>>
More information about the Pyrex
mailing list