Cython扩展类型属性误解
Cython extension types attributes misunderstanding
我试图为cython类中的一个成员授予python访问权限。成员类型是基本的(例如int
或float
)
正如我在文档中所读到的,您可以使用属性来访问底层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
现在这就行了。
但是,据我所知,对于基本类型,您可以只使用扩展类型。在这种情况下,您可以使成员public
可访问和/或可写。您不需要属性:
cdef class myPythonClass:
cdef public int myCppMember # direct access to myCppMember
但当我使用第二个选项时,它不起作用。该变量永远不会更新。是我遗漏了什么,还是我没有完全理解?
感谢您的投入。
您已经找到了解决方案,使用property
是最好的方法。
public
属性可以在类方法之外访问,而private
属性只能由类方法使用。
但是,即使是在C++级别定义的public
属性也不能从Python访问。使用property
公开private
或public
属性将使其可用于Python。
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- C++概念:如何使用'concept'检查模板化结构的属性?
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 通过指向指针数组的指针访问子类的属性
- MSVC是否支持C++11样式的属性而不是__declspec
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 类的C++属性似乎已重新初始化
- Qt Quick-如何仅从c++代码与qml属性交互
- 用概念检查属性的类型
- C++如何将同一类的实例作为属性
- 按多个属性排序
- 主函数参数的属性
- "perf_event_attr"结构的"read_format"属性的选项到底是什么?
- C++删除未使用的类属性会导致 std::logic_error
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- 使用内存地址访问结构的属性值
- C++调用具有 *this 属性的单个帮助程序函数
- C++ 在堆栈中包含多态属性的类对象存储
- C++ 命名参数习惯用语 - 未设置字符串属性
- Cython扩展类型属性误解