[Pyrex] How to specify __contains__ method?
Gary Bishop
gb at cs.unc.edu
Sun Apr 11 20:43:02 CEST 2004
When I compile the following simple example:
cdef class foo:
def __contains__(self, key):
return 1
I get the error message:
d:\msl\gb\python\my\Matrix\bug2.pyx:3:11: Return with value in void function
If I change "def" to "cdef int", it gets past Pyrex but the C compiler
complains:
bug2.c(147) : warning C4028: formal parameter 1 different from declaration
But it appears to work.
Now in a more complicated case, I can't get it to work properly.
cdef class foo:
cdef int val
def __init__(self):
val = 0
cdef int __contains__(self, key):
print 'foo contains', self.val
return 1
cdef class fee(foo):
cdef int __contains__(self, key):
print 'fee contains', self.val
return 0
cdef class faa(fee):
def bof(self):
print 'bof', self.val
cdef int __contains__(self, key):
print 'faa contains', self.val
return 0
If I leave off that last cdef int __contains__, I get C compiler errors like:
bug2.c(275) : warning C4028: formal parameter 1 different from declaration
bug2.c(421) : warning C4028: formal parameter 1 different from declaration
bug2.c(568) : error C2065: 'None' : undeclared identifier
bug2.c(568) : error C2099: initializer is not a constant
With it included, it will compile but when I try to do
import bug2
faa = bug2.faa()
1 in faa
I get:
faa contains
Exception exceptions.AttributeError: "'bug2.faa' object has no
attribute 'val'" in 'bug2.faa.__contains__' ignored
I seem to have lost my connection (via inheritance) to the original foo class.
I poked around in the Pyrex0.9 source a little but couldn't find my way
to where this might happen.
Any ideas for a work around?
Thanks for Pyrex, it is GREAT STUFF.
gb
More information about the Pyrex
mailing list