2
如何我必须用Cython重写RMUL?正确覆盖
例如,这完全在蟒蛇
class PyFoo:
def __init__(self):
self.a = 4
def __mul__(self,b): return b*self.a
def __rmul__(self,b): return self*b
Pynew = PyFoo()
print " Python "
print Pynew*3 # I got 12
print 3*Pynew # I got 12
但是,如果我实现用Cython同样不起作用,
cclass.pyx
cimport cython
cdef class Foo:
cdef public int a
def __init__(self):
self.a = 4
def __mul__(self,b): return b*self.a
def __rmul__(self,b): return self*b
test.py
import cclass as c
Cnew = c.Foo()
print " Cython "
print Cnew*3 # This works, I got 12
print 3*Cnew # This doesn't
我得到这个错误
Traceback (most recent call last):
File "test.py", line 22, in <module>
print 3*Cnew
File "cclass.pyx", line 8, in cclass.Foo.__mul__ (cclass.c:763)
def __mul__(self,b): return b*self.a
AttributeError: 'int' object has no attribute 'a'
我不明白什么是与使用的问题在用Cython相同的实现RMUL的。
谢谢!是的,我很内疚。我没有查看文档,我在谷歌搜索了很多(通常我得到更好的答案,当问题是,一定是行不通的事情)。 我的歉意,再次感谢。 – diego