【发布时间】:2019-04-24 12:16:53
【问题描述】:
在 IPython 中,我可以看到 Cython 类的属性是一个生成器,只需定义它然后调用:
%%cython
cdef class SomeCls:
property x:
def __get__(self):
yield 1
电话看起来像
SomeCls().x
# prints <generator at 0x102f61ee8>
我无法测试该属性是否是生成器:
import types
print(isinstance(SomeCls().x, types.GeneratorType))
# prints False
import inspect
print(inspect.isgeneratorfunction(SomeCls.x))
# prints False
如何确定 Cython 类的属性是否是生成器?
【问题讨论】:
-
避免在 cmets 中回答问题。
-
当 not 使用 cython 时,针对
types.GeneratorType的测试工作得很好... -
不要挑剔从哪里获得帮助!作为一个经验丰富的发帖者,我经常用简短的 cmets 来回答。对于常规答案,我会花更多时间提供工作示例和解释。
-
这个没有解决了这个问题,但是 Cython 现在支持标准的
@property语法(我认为现在是首选,因为它与 Python 的其余部分相匹配)。它的行为与 this 完全相同。
标签: python types generator cython isinstance