cython return语句给出了一个内置方法而不是整数

时间:2016-03-11 01:10:02

标签: python c++ cython

我正在使用cython来包装我的库。我是一名新手程序员,所以这给了我一些成长的痛苦。我现在正在阅读示例,我已经使用Rectangle.h和rect.pyx以及setup.py为Rectangle.cpp类编译了一个包装库。现在我用test.py

运行它

我有一个 rect.pyx 文件:

# distutils: language = c++
# distutils: sources = Rectangle.cpp

cdef extern from "Rectangle.h" namespace "shapes":
    cdef cppclass Rectangle:
        Rectangle(int, int, int, int) except +
        int x0, y0, x1, y1
        int getLength()
        int getHeight()
        int getArea()
        void move(int, int)

cdef class PyRectangle:
    cdef Rectangle *thisptr      # hold a C++ instance which we're wrapping
    def __cinit__(self, int x0, int y0, int x1, int y1):
        self.thisptr = new Rectangle(x0, y0, x1, y1)
    def __dealloc__(self):
        del self.thisptr
    def getLength(self):
        return self.thisptr.getLength()
   def getHeight(self):
        return self.thisptr.getHeight()
    def getArea(self):
        return self.thisptr.getArea()
   def move(self, dx, dy):
        self.thisptr.move(dx, dy)

我已将其构建到库中,并使用此文件调用它 test.py

from rect import PyRectangle

rec = PyRectangle(0,0,4,4)

length = rec.getLength
print(length)

但是当我运行这个时,我得到的是<built-in method getLength of rect.PyRectangle object at 0x7fb174f19240>

为什么我没有返回int?我该怎么做?

1 个答案:

答案 0 :(得分:2)

调用您的方法:

改变这个:

rec.getLength

到此:

rec.getLength()