将 Cython 属性公开给 Python 的 Pythonic 方法是什么?
What's the Pythonic way to expose Cython attributes to Python?
我正在尝试制作一个 Cython class,它具有一些需要从 Python 访问的属性。现在,我能想到的最简洁的方法是使用 @property
来显式定义 getter 和 setter,如下所示:
@property
def some_attr(self):
return self._some_attr
@some_attr.setter
def some_attr(self, unicode val):
self._some_attr = val
对我来说,这感觉像是很多无关和冗长的样板。在 Cython 中是否没有内置的方法来使用装饰器来做到这一点?
将属性定义为可在 Python 级别访问的关键字是 public
:
cdef class Foo:
cdef public unicode some_attr
...
我正在尝试制作一个 Cython class,它具有一些需要从 Python 访问的属性。现在,我能想到的最简洁的方法是使用 @property
来显式定义 getter 和 setter,如下所示:
@property
def some_attr(self):
return self._some_attr
@some_attr.setter
def some_attr(self, unicode val):
self._some_attr = val
对我来说,这感觉像是很多无关和冗长的样板。在 Cython 中是否没有内置的方法来使用装饰器来做到这一点?
将属性定义为可在 Python 级别访问的关键字是 public
:
cdef class Foo:
cdef public unicode some_attr
...