python - Cython,无法访问魔术算术函数中的类成员
问题描述
考虑以下测试类:
cdef class Test:
cdef int a
def __cinit__(self, a):
self.a = a
def __add__(self, other):
return Test(self.a + other.a)
编译并执行
import test
t = test.Test(1)
t2 = test.Test(2)
t3 = t + t2
产生以下错误
traceback (most recent call last):
File "test.py", line 19, in <module>
t3 = t + t2
File "test.pyx", line 285, in test.Test.__add__
return Test(self.a + other.a)
AttributeError: 'test.Test' object has no attribute 'a'
为什么会这样?到目前为止,以类似的方式实现其他魔法功能,如复制,效果很好。
解决方案
推荐阅读
- javascript - 如何仅在更改站点时重定向站点的端口更多**主题中的详细信息**
- thymeleaf - thymeleaf 的默认 else 语句 if case
- javascript - Javascript:从字符串数组创建字典
- swift - 为什么我在 ViewController.swift 文件中看不到要添加到情节提要的对象?
- javascript - ReactJs 搜索按钮在 fetch 之前重新加载页面可以获取图像链接并显示它们
- java - 检查自动化 BPMN 任务的数据流异常
- apache-spark - DataFrame 元素的成对比较
- c# - Visual Studio 2019 将 C# 程序导出为 DLL 而不是 EXE?
- java - 如何在 Codename one 中更改 Google Maps api 中路径的样式
- typescript - 测试停止,对 element.isDisplayed 进行条件操作,并且不显示元素