【问题标题】:cython inheritancecython 继承
【发布时间】:2019-03-27 06:43:56
【问题描述】:

我有一个 A.pxd(仅包含函数声明)和 A.pyx,其中仅包含一个包含所有函数体的类 A。

比我有从 A 继承的 B,

对于 B,我有 B.pxd 和一些功能

B.pyx

class Bclass(A):
    #all the funcions body

我现在想知道如何告诉 B.pyx 将 A 识别为类型名称?

我要做的是:

B.pyx

cimport A
import A
from A import Aclass
cdef Bclass(Aclass):
   #body

但它告诉我:A 不是类型名称

如果我只在一个 file.pyx 中执行此操作,它可以正常工作,但使用 files.pxd 它不会。

【问题讨论】:

    标签: inheritance cython


    【解决方案1】:

    使用

    from A cimport Aclass
    cdef class Bclass(Aclass):
        # ...
    

    cimport A
    cdef class Bclass(A.Aclass):
        # ...
    

    注意Aclass 必须是cdef'fed 类,Cython extension types 不能从 Python 类继承。

    【讨论】:

    • 当我执行 cimport A 时,这意味着我正在导入 A.pxd,对吧?
    • 它不再给我的参数 Bclass(A.Aclass) 带来错误,但是当我将 Btype 转换为 Atype 时,它​​说: 不是类型标识符。当然 被定义为 A.pxd 中的 ctypedef 结构,我应该专门导入一些东西吗?
    • castinA 是什么?请改用<Aclass>myBclassInstance
    • 那么请将问题标记为已回答。我很高兴能帮助你。 :)
    猜你喜欢
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多