python - 在 cdef 类中调用 cdef

标签 python cython

在不牺牲 cdef 调用程序中的 cdef 的情况下,他们有什么方法可以使这项工作正常进行吗? (也没有使用 cpdef)

from array import *
from numpy import *
cdef class Agents:
    cdef public caller(self):
        print "caller"
        A[1].called()

    cdef called(self):
        print "called"


A = [Agents() for i in range(2)]

def main():
    A[0].caller()

最佳答案

对于 Cython,A[1] 将是一个 python 对象。如果您希望仍然能够使用 cdef,请在调用者中使用自动转换:

cdef public caller(self):
    cdef Agents agent
    print "caller"
    agent = A[1]
    agent.called()

您可以使用 cython 中的 -a 模式进行检查,以了解您对每一行代码使用的是 Python 还是 C。 (cython -a yourfile.pyx -> 将生成一个你可以浏览和检查的 yourfile.html)。

关于python - 在 cdef 类中调用 cdef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2444000/

相关文章:

python - 将文件句柄传递给 cython 函数

python - 用于发送邮件的 crontab

python - 如何替换数据框中的 Year 并在 Pandas 中将该值乘以 12

python - 使用正则表达式

python - 在 Cython 中将 C++ vector 转换为 numpy 数组而无需复制

python - 将指针传递给 Cython 中的构造函数

python - 如何以编程方式识别网站结构的变化

python - 在 Django 中不属于您网站的抽象网址

python - Cython:了解具有间接连续内存布局的类型化内存 View

cython - 如何从 C 访问 asyncio/uvloop 循环