简体   繁体   中英

Using cython .pxd files to Augment pure python files

Following the example here, " Augementing .pxd ", I'm trying to use ".pxd" files to augment a pure python file. (Add type definitions external to the pure python file).

python file:

class A(object):
    def foo(self, i=3, x=None):
        print "Big" if i > 1000 else "Small"

pxd file:

cdef class A:
    cpdef foo(self, int i, x)

I've got a dictionary, which I'm defaulting to "None" in python. Unfortunately, cython doesn't like this.

If I use my "pure" python file, without declaring a type or declare the type as "dict" in the pxd file I get the error:

"Signature not compatible with previous declaration"

I noticed that it will compile if I do NOT specify a default value, but there's a reason for declaring the defaults.

Is there a way this can be handled?

Optional arguments in cpdef functions are declared differently from cdef functions which essentially is same as python functions.

Your .pxd file should be modified to be written as

cdef class A:
    cpdef foo(self, int i=*, x=*)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM