【发布时间】:2014-10-21 18:31:19
【问题描述】:
我正在尝试为 cython 类中的成员授予 python 访问权限。成员类型是基本的(例如int 或float)
正如我在文档中所读到的,您可以使用 properties 来授予对底层 C++ 成员的访问权限:
cdef class myPythonClass:
# grant access to myCppMember thanks to myMember
property myMember:
def __get__(self):
return self.thisptr.myCppMember # implicit conversion
# would somehow be the same logic for __set__ method
现在可以了。
但是,据我了解,对于基本类型,您可以使用extension types。在这种情况下,您创建成员 public 以使其可访问和/或可写。你不需要属性:
cdef class myPythonClass:
cdef public int myCppMember # direct access to myCppMember
但是当我使用第二个选项时,它不起作用。变量永远不会更新。有什么我遗漏或没有完全理解的吗?
感谢您的意见。
【问题讨论】:
-
你能举个例子说明它没有像你期望的那样工作吗?
-
@carmellose 您还可以在 Cython 中使用外部
def函数来提取public属性,但不能提取private属性(对于后者,您必须使用类方法,类似于property方法)