<br><div><span class="gmail_quote">On 9/26/06, <b class="gmail_sendername">Greg Ewing</b> &lt;<a href="mailto:greg.ewing@canterbury.ac.nz">greg.ewing@canterbury.ac.nz</a>&gt; 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>&gt; The main problem comes when I get another object of the<br>&gt; same class, e.g. for comparison or arithmetic operations.<br>&gt; I can access self.my_struct but not other.my_struct,<br><br>You need type declarations. For example,
<br>...<br>&nbsp;&nbsp;&nbsp;&nbsp; def __add__(x, y):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef myclass my_x<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp;&nbsp;&nbsp;&nbsp; def __add__(myclass x, myclass y):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br><br>i.e. don't assume that the first argument is &quot;self&quot;<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 &quot;self&quot;-like.<br>Please correct me if that is not the case.<br><br>I've settled on:<br>&nbsp; cdef myclass my_x, my_y<br>&nbsp; if not isinstance(x, myclass):<br>&nbsp;&nbsp;&nbsp; my_x = myclass(x)
<br>&nbsp; if not isinstance(y, myclass):<br>&nbsp;&nbsp;&nbsp; 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 &quot;stdlib.h
&quot;', you should find<br></blockquote><div><br>You're right - I had been trying to get size_t from<br>somewhere else.<br>&nbsp;</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
&gt; In the spirit of &quot;batteries included&quot; couldn't Pyrex just parse<br>&gt;&nbsp;&nbsp; cdef int *c_array[len(input)]<br>&gt; and do its own memory allocation and garbage collection in the<br>&gt; 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>