<br><div><span class="gmail_quote">On 9/26/06, <b class="gmail_sendername">Greg Ewing</b> <<a href="mailto:greg.ewing@canterbury.ac.nz">greg.ewing@canterbury.ac.nz</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Adapted Cat wrote:<br>> The main problem comes when I get another object of the<br>> same class, e.g. for comparison or arithmetic operations.<br>> I can access self.my_struct but not other.my_struct,<br><br>You need type declarations. For example,
<br>...<br> def __add__(x, y):<br> cdef myclass my_x<br> cdef myclass my_y</blockquote><div><br>Thanks - that worked! <br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Note that if you're implementing a binary operator<br>method such as __add__, you'll need<br><br> def __add__(myclass x, myclass y):<br> ...<br><br>i.e. don't assume that the first argument is "self"<br>
(see the Special Methods of Extension Types pages for<br>a full explanation).</blockquote><div><br>Yes, I saw that note. With regard to __sub__ as I understand it<br>it is always the case that __sub__(one, two) should return one - two
<br>and not two - one...even in the case where one is not "self"-like.<br>Please correct me if that is not the case.<br><br>I've settled on:<br> cdef myclass my_x, my_y<br> if not isinstance(x, myclass):<br> my_x = myclass(x)
<br> if not isinstance(y, myclass):<br> my_y = myclass(y)<br></div><br>This way if I can coerce it it'll work and if I can't coerce<br>it then the __init__ will raise the appropriate exception.<br><br><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
This only happens when the ctypedef is inside a<br>'cdef extern from' block. If you do it the way<br>you did in your posted code, with both the<br>ctypedef and the malloc declaration inside<br>'cdef extern from "stdlib.h
"', you should find<br></blockquote><div><br>You're right - I had been trying to get size_t from<br>somewhere else.<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
> In the spirit of "batteries included" couldn't Pyrex just parse<br>> cdef int *c_array[len(input)]<br>> and do its own memory allocation and garbage collection in the<br>> generated C code?<br><br>
In the simplistic sense of treating it as an<br>automatic variable and freeing it on exit from<br>the function, this might be doable.</blockquote><div><br>Yes, that's what I had in mind.<br>Thanks again for your help!<br>
</div></div><br>