[Cython] __add__ type declaration
Robert Bradshaw
robertwb at math.washington.edu
Sun Nov 16 00:47:52 CET 2008
On Nov 15, 2008, at 3:39 PM, Benjamin Peterson wrote:
> Hi!
>
> For fun, I have been implementing a Python binding to GMP. I have a
> class like
> this:
>
> cdef class GMPInt:
>
> cdef mpz_t ob_val
> ....
> def __add__(self, GMPInt other):
> cdef mpz_t temp
> mpz_init(temp)
> mpz_add(temp, self.ob_val, other.ob_val)
> mpz_clear(temp)
> ....
>
> When I try to compile it I get this error:
>
> mpz_add(temp, self.ob_val, other.ob_val)
> ^
> ------------------------------------------------------------
>
> /temp/sandbox/pygmp/src/_gmp.pyx:49:27: Cannot convert Python
> object to 'mpz_t'
>
>
> It compiles correctly when I define a declaration for self in __add__
> like this, though:
>
> def __add__(GMPInt self, GMPInt other)
>
> I thought type declarations were supposed to be implicit on self in
> classes. Is this just an anomaly?
>
> I have Cython 0.10.
>
> Thanks for the help!
> --
That is correct most of the time. What you're observing here due to
the fact that the __add__ method may take self in either the left or
right parameter. (This is how the slot works in extension classes,
rather than having a separate __radd__ method.)
We really need to add that to
http://docs.cython.org/docs/extension_types.html#special-methods
- Robert
More information about the Cython-dev
mailing list