Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Cython:在类型声明中使用导入的类_Python_Cython - Fatal编程技术网

Python Cython:在类型声明中使用导入的类

Python Cython:在类型声明中使用导入的类,python,cython,Python,Cython,我正在编写一个Cython 0.23程序,我不知道如何在类型声明中使用从不同模块导入的cdef类。下面是一个重现问题的片段 test.py: import pyximport pyximport.install() from mymodule import * obj = MyClass(42) print(obj.field) print(identity(obj).field) 这将按预期工作,并打印两次42: mymodule.pyx: cdef class MyClass:

我正在编写一个Cython 0.23程序,我不知道如何在类型声明中使用从不同模块导入的
cdef类。下面是一个重现问题的片段

test.py

import pyximport
pyximport.install()

from mymodule import *

obj = MyClass(42)
print(obj.field)
print(identity(obj).field)

这将按预期工作,并打印两次
42

mymodule.pyx

cdef class MyClass:
    cdef readonly int field
    def __init__(self, field):
        self.field = field

cpdef MyClass identity(MyClass obj):
    return obj
from utils import MyClass

cpdef MyClass identity(MyClass obj):
    return obj
cdef class MyClass:
    cdef readonly int field
    def __init__(self, field):
        self.field = field

此操作失败,并出现编译器错误:

mymodule.pyx

cdef class MyClass:
    cdef readonly int field
    def __init__(self, field):
        self.field = field

cpdef MyClass identity(MyClass obj):
    return obj
from utils import MyClass

cpdef MyClass identity(MyClass obj):
    return obj
cdef class MyClass:
    cdef readonly int field
    def __init__(self, field):
        self.field = field
utils.pyx

cdef class MyClass:
    cdef readonly int field
    def __init__(self, field):
        self.field = field

cpdef MyClass identity(MyClass obj):
    return obj
from utils import MyClass

cpdef MyClass identity(MyClass obj):
    return obj
cdef class MyClass:
    cdef readonly int field
    def __init__(self, field):
        self.field = field

错误:

Error compiling Cython file:
------------------------------------------------------------
...
from utils import MyClass

cpdef MyClass identity(MyClass obj):
     ^
------------------------------------------------------------

mymodule.pyx:3:6: 'MyClass' is not a type identifier

Error compiling Cython file:
------------------------------------------------------------
...
from utils import MyClass

cpdef MyClass identity(MyClass obj):
                      ^
------------------------------------------------------------
我需要它的项目很小,我可以重构它,这样我就不需要导入类,但是这个解决方案看起来不是很干净。有更好的方法吗?

您需要和
cimport
。(本质上,
cimport
发生在编译时,而
import
发生在运行时,因此Cython无法使用任何导入的内容)

创建“utils.pxd”:

“utils.pyx”现在是

cdef class MyClass:
  def __init__(self, field):
    self.field = field
(即删除
字段
的声明,因为它在.pxd文件中指定)

然后在
mymodule.pyx中

from utils cimport MyClass
# other code follows...

非常感谢。有没有办法把类的定义放在一个地方?我不这么认为(我同意-这个方法有点麻烦)。@DavidW-这里的文档中-我们现在需要cdef CPPCClass吗?我显然会问我自己的问题,因为我得到了同样的编译错误<代码> CDEF CPPPclass < /Cord>在包装C++中定义的类时使用。code>cdef类
定义了一个可以从Python中使用的类,但是使用Cython类型定义,所以它们可以做不同的事情。在这两种情况下,它们都可以被移植到另一个Cython模块中使用。