On Apr 28, 2008, at 11:55 AM, Brian Blais wrote:

> Hello,
>
> I must be missing something minor but I just can't figure it out.  I
> am trying to replace a method in a class with a cdef function defined
> in cython, but keep getting the error:
>
> TypeError: function takes exactly 2 arguments (1 given)
>
>
> I include an example to reproduce the issue, with the .py file and
> the .pyx file, below.  Any help would be great!

Somehow, when you do

     class spam:
         pass

     spam.eggs = foo

Foo gets magically turned into a classmethod if it is a python  
function, but not if it is a "builtin" python function. (Seems like  
poor design to me.) To see this in pure Python, do

     spam.len = len
     spam().len()

which will be a similar error. You need to write

     mything.doit = classmethod(testit.doit)

- Robert

>
>
> thanks,
>
>                       Brian Blais
>
> 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=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

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

Reply via email to