【问题标题】:Definition of def, cdef and cpdef in cythoncython中def、cdef和cpdef的定义
【发布时间】:2015-04-06 08:57:34
【问题描述】:

当我声明一个函数时,我想知道defcdefcpdef 之间的区别。 def 和其他之间的区别或多或少是清楚的。 而且我还看到,有时它会在声明中添加返回类型 (cdef void/double/int... name),有时不会。

我也想知道如何在cython中声明一个字符串变量,因为我不知道,所以我把它声明为对象。

【问题讨论】:

    标签: python declaration cython


    【解决方案1】:

    主要区别在于函数的调用位置:def 函数可以从 Python 和 Cython 调用,而 cdef 函数可以从 Cython 和 C 调用。

    这两种类型的函数都可以使用类型化和非类型化参数的任意混合来声明,并且在这两种情况下,内部都由 Cython 编译为 C(编译后的代码应该非常非常相似):

    # A Cython class for illustrative purposes
    cdef class C:
       pass
    
    def f(int arg1, C arg2, arg3):
        # takes an integer, a "C" and an untyped generic python object
        pass
    
    cdef g(int arg1, C arg2, arg3):
        pass
    

    在上面的示例中,f 将对 Python 可见(一旦它导入了 Cython 模块),g 将不会也不能从 Python 调用。 g 将转换为 C 签名:

    PyObject* some_name(int, struct __pyx_obj_11name_of_module_C *, PyObject*)
    

    (其中struct __pyx_obj_11name_of_module_C * 只是我们的类C 被翻译成的C 结构)。例如,这允许它作为函数指针传递给 C 函数。相比之下,f 不能(轻松)从 C 中调用。

    cdef 函数的限制:

    cdef 函数不能在其他函数中定义 - 这是因为无法将任何捕获的变量存储在 C 函数指针中。例如。以下代码是非法的:

    # WON'T WORK!
    def g(a):
       cdef (int b):
          return a+b
    

    cdef 函数不能接受 *args**kwargs 类型的参数。这是因为它们不容易被翻译成 C 签名。

    cdef 函数的优点

    cdef 函数可以接受任何类型的参数,包括那些没有 Python 等价物的参数(例如指针)。 def 函数不能有这些,因为它们必须可以从 Python 中调用。

    cdef 函数还可以指定返回类型(如果未指定,则返回 Python 对象,C 中的 PyObject*)。 def 函数总是返回一个 Python 对象,所以不能指定返回类型:

    cdef int h(int* a):
        # specify a return type and take a non-Python compatible argument
        return a[0]
    

    cdef 函数比 def 函数调用更快,因为它们转换为简单的 C 函数调用。

    cpdef 函数

    cpdef 函数使 Cython 生成一个 cdef 函数(允许从 Cython 进行快速函数调用)和一个 def 函数(允许您从 Python 调用它)。 def 函数内部只是调用 cdef 函数。在允许的参数类型方面,cpdef 函数具有cdefdef 函数的所有限制。

    何时使用cdef 函数

    一旦函数被调用,cdefdef 函数内的代码运行速度没有区别。因此,仅在以下情况下使用cdef 函数:

    • 您需要传入或传出非 Python 类型,或者
    • 您需要将它作为函数指针传递给 C,或者
    • 您经常调用它(因此加速函数调用很重要),您不需要从 Python 中调用它。

    在您经常调用cpdef 函数时使用它(因此加速函数调用很重要),但您确实需要从 Python 中调用它。

    【讨论】:

    • 我在这里解释并重新使用了你的答案:stackoverflow.com/questions/49172528/…你能再检查一下吗?
    • @PDiracDelta 我确实看到了。它看起来像是一个有用的问题/答案组合(在我看来,很多人使用cdef 是因为他们认为它会自动更快地没有真正的原因,所以最好有一个简单的总结)。我在那里看不到任何我不同意的地方
    【解决方案2】:

    def 在 Python 中声明一个函数。由于 Cython 基于 C 运行时,它允许您使用 cdefcpdef

    cdef在C语言层声明函数。如您所知(或不知道?)在 C 语言中,您必须为每个函数定义返回值的类型。有时函数返回 void,这与 Python 中的 return 相同。

    Python 是一种面向对象的语言。所以你也可以在 C++ 语言层定义类方法,并在子类中重写这些方法:

    cdef class A:
        cdef foo(self):
            print "A"
    
    cdef class B(A)
        cdef foo(self, x=None)
            print "B", x
    
    cdef class C(B):
        cpdef foo(self, x=True, int k=3)
            print "C", x, k
    

    总结一下,为什么我们需要使用defcdefcpdef?因为如果你使用 Cython,你的 Python 代码会在编译前转换成 C 代码。因此,您可以使用这些东西控制生成的 C 代码列表。

    更多信息建议你阅读官方文档:http://docs.cython.org/src/reference/language_basics.html

    【讨论】:

    • Python 代码永远不会被转换为C,它只是被转换为python bytecode 并由用C 编写的python 解释器进行评估。
    • @gsb-eng 我们谈论的是 Cython,而不是 Python。 Cython works by producing a standard Python module. However, the behavior differs from standard Python in that the module code, originally written in Python, is translated into C.en.wikipedia.org/wiki/Cython#Design
    • 答案没有描述 cpdef 与 cdef 的区别
    猜你喜欢
    • 2018-07-29
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多