3

I am trying to Cython to speed up some class. But I still want the code to run in pure Python too.

How do I define an array in an class (code has been simplified)

import cython

class A:
    def __init__(self):


        if cython.compiled:

            # This will work in Cython
            for k in len(self.S):
               self.S[k]=k

        else:
            # This will work in interpreter
            self.S=range(8)

    def test(self):
         self.S[0]+=1

And in the .pxd:

 import cython

 cdef class A
     cdef int[8] S

     cdef test(self)

But Cython complains on compilation:

Cannot convert Python object to 'int [8]'
3
  • Just a comment, not directly related to your question, but if you want the cython-compiled part to fully take advantage of Cython's speedup, you should always cdef your increment variables as well: cdef k... Commented Dec 21, 2013 at 11:40
  • 1
    My goal was to speed up the test method. which (is in the un simplified version is more complex) is called millions of times, while init is only called once, Commented Dec 21, 2013 at 11:51
  • Sure, I got that. That was a very general remark targeting other people who are susceptible to read this thread. Cheers ;) Commented Dec 21, 2013 at 14:35

2 Answers 2

3

I finally got it to work:

import array

class A:
    def __init__(self):

        # This will work in Cython
        self.S=array.array("l", range(8))

    def test(self):
         self.S[0]+=1

And .pxd:

cimport cpython.array

cdef class RC4:
    cdef int [:] S
    cdef int test(self)
Sign up to request clarification or add additional context in comments.

1 Comment

The second function should be test(), not next().
0

This is because the syntax is wrong, more like:

cdef int S[8]

Also, there's no need to import cython.

This is actually defined in the very beginning of the documentation for cython.

6 Comments

Nope. The error message does not change. The problem seems to be, that I must somehow initialize self.S in pure Python without causing a type conflict during compile (or for Cython to ignore the else case). My code worked without the CPython initializer in Cython
@RuedigerJungbeck Try creating a separate file, where the only thing you do is make a cdef int S[8], and see if cython can recognize that, since I'm 100% sure that the above syntax is correct.
@RuedigerJungbeck My advice is to keep cython completely separate from Python, and just import the functions or classes that you require. I say this because cython has many quirks and most of them come from when you try to use them together too much. Also you might try using cpdef instead of cdef.
That is what I did with the .pxd file. My Problem is that I habe statementwhich initializes S in pure Python. And when I try so (conditionally) Cython refuses to compile.
@RuedigerJungbeck Thats to be expected. I'm sorry that you can't have what you want. To be honest, I also discourage writing apps in pure cython (my own experience), its better to write them in C/C++, and then import them into cython (to wrap them). I say this from experience because, cython has many quirks I'm afraid.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.