On Apr 28, 2008, at 1:34 PM, Brian Blais wrote:
>
> On Apr 28, 2008, at Apr 28:3:13 PM, Robert Bradshaw wrote:
>
>>  You need to write
>>
>>     mything.doit = classmethod(testit.doit)
>>
>> - Robert
>>>
>
>
> thanks, but now I get a different error.  :)  It feels like I am  
> missing some bit of python "magic", which I find annoying given  
> that usually python is "the way I think is the way it is".

Yes, the way Python deals with (bound/unbound) instance methods,  
functions, builtin methods, etc. is not one of the most transparent  
parts of Python... I misspoke when I said use classmethod. What you  
nee do to is

     import types
     mything.doit  = types.MethodType(testit.doit, None, mything)

Don't ask me how you were supposed to figure this out short of being  
intimately familiar with how Python internals work...

- Robert

>
>
>               thanks again,
>
>
>                               bb
>
> The full error is:
>
> ---------------------------------------------------------------------- 
> -----
> AttributeError                            Traceback (most recent  
> call last)
>
> /Users/bblais/python/testit.py in <module>()
>      22
>      23     a=mything(10)
> ---> 24     print a.doit(50)
>      25
>      26
>
> /Users/bblais/python/c_testit.so in c_testit.doit (c_testit.c:216)()
> ----> 1
>       2
>       3 cpdef doit(object self,int x):
>       4
>       5     cdef int i
>       6     cdef int N=self.N
>       7     cdef int val
>
> /Users/bblais/python/c_testit.so in c_testit.doit (c_testit.c:137)()
>       2
>       3     cdef int i
> ----> 4     cdef int N=self.N
>       5     cdef int val
>       6
>
> AttributeError: type object 'mything' has no attribute 'N'
> WARNING: Failure executing file: <testit.py>
>
>
> The code is:
>
> =============================
> #testit.py
>
> class mything(object):
>
>     def __init__(self,N):
>         self.N=N
>
>     def doit(self,x):
>
>         val=0
>         for i in range(self.N,x):
>             val+=i
>
>         return val
>
> try:
>     import c_testit
>     mything.doit=classmethod(c_testit.doit)
>     print "Using cython version"
> except ImportError:
>     pass
>
> if __name__=="__main__":
>
>     a=mything(10)
>     print a.doit(50)
>
> =============================
> # c_testit.pyx
>
>
> cpdef doit(object self,int x):
>
>     cdef int i
>     cdef int N=self.N
>     cdef int val
>
>     val=0
>     for i from N<=i<x:
>         val+=i
>
>     return val
>
>

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to