1
在以下代码中,如何使用纯Python语法在cython中注释self.a
的类型?类变量的类型注释
class C:
def __init__(self):
self.a = 1
@cython.locals(x = int)
def f(self, x):
self.a += x
在以下代码中,如何使用纯Python语法在cython中注释self.a
的类型?类变量的类型注释
class C:
def __init__(self):
self.a = 1
@cython.locals(x = int)
def f(self, x):
self.a += x
我下面的作品,均与用Cython 0.16和0.19.1
import cython
@cython.cclass
class C(object):
a=cython.declare(cython.int)
def __init__(self):
self.a = 1
@cython.locals(x = int)
def f(self, x):
self.a += x
def val(self):
return self.a
正如你可以看到cython.declare
应该在课堂上使用,而不是__init__
。
你有没有尝试类似'self.a = cython.declare(cython.int); self.a = 1'? – mgilson 2013-04-30 19:58:15
@mgilson不起作用。错误是'申报'不是一个有效的cython属性或正在被错误地使用 – leon 2013-04-30 20:00:28
@leon:您用于mgilson评论的实际代码是什么?因为对于我来说,使用Cython 0.19,无论是Python 2.7.2还是3.3.0,将该行放在'__init__'中都是有效的(因为它在纯Python中没有效果,除了在将'self.a'设置为'0'之前设置为“1”)。 – abarnert 2013-04-30 20:03:24