1

在下面的代码中,如何使用纯 python 语法来注释self.acython 中的类型?

class C:
  def __init__(self):
    self.a = 1
  @cython.locals(x = int)
  def f(self, x):
    self.a += x
4

1 回答 1

0

以下适用于我,适用于 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__.

于 2013-07-15T09:10:30.320 回答