What are the differences between a cpdef and a cdef wrapped in a def?

前端 未结 2 636
春和景丽
春和景丽 2020-12-16 03:48

In the Cython docs there is an example where they give two ways of writing a C/Python hybrid method. An explicit one with a cdef for fast C access and a wrapper def for acce

2条回答
  •  攒了一身酷
    2020-12-16 04:23

    See docs here - for most purposes they are practically the same, cpdef has slightly more overhead but plays nicer with inheritance.

    The directive cpdef makes two versions of the method available; one fast for use from Cython and one slower for use from Python. Then:

    This does slightly more than providing a python wrapper for a cdef method: unlike a cdef method, a cpdef method is fully overridable by methods and instance attributes in Python subclasses. It adds a little calling overhead compared to a cdef method.

提交回复
热议问题