【发布时间】:2021-05-12 12:15:10
【问题描述】:
Boost Python 有一个非常简单的方法来包装重载运算符。 boost.org (https://www.boost.org/doc/libs/1_66_0/libs/python/doc/html/tutorial/tutorial/exposing.html) 上公开 C++ 类、方法等的教程给出了这个例子:
示例类 FilePos 的重载 C++ 运算符:
class FilePos { /*...*/ };
FilePos operator+(FilePos, int);
FilePos operator+(int, FilePos);
int operator-(FilePos, FilePos);
FilePos operator-(FilePos, int);
FilePos& operator+=(FilePos&, int);
FilePos& operator-=(FilePos&, int);
bool operator<(FilePos, FilePos);
如何将它们映射到 Python:
class_<FilePos>("FilePos")
.def(self + int()) // __add__
.def(int() + self) // __radd__
.def(self - self) // __sub__
.def(self - int()) // __sub__
.def(self += int()) // __iadd__
.def(self -= other<int>())
.def(self < self); // __lt__
我有一个名为“Angle”的 C++ 类,它代表一个角度。它具有重载的运算符,包括“+=”、“-=”、“*=”和“/=”。每个重载运算符都旨在将某些 double 类型的输入与 Angle 类所持有的度数相加、减、乘或除。 Angle 类包装器以与上面示例中描述的完全相同的方式映射这些重载运算符。当我在 Python 中测试加法、减法和乘法运算符时,它们工作得非常好。除法运算符,并非如此。当我像这样运行一个简单的测试时:
A = Angle(1) # instantiate Angle object with magnitude of 1 radian
A /= 2.0 # attempt to divide the radian measurement by 2
我收到以下错误:
TypeError: unsupported operand type(s) for /=: 'Angle' and 'float'
基于这个问题:
Error when trying to overload an operator "/"
我看到 Python3(我正在使用的版本)对“/”字符有一种独特的理解方式。但是,这个解决方案对我不起作用,因为我使用的类是一个封装的 C++ 类。
我的问题:有没有办法使用 boost 以保留相同语法的方式将重载的运算符“/=”从 C++ 映射到 python(即不在 C++ 中编写一个名为“selfdiv”的瘦包装器来执行相同的操作,但必须用“Angle.selfdiv()”调用)?也许某种方式可以覆盖 Python 解释该正斜杠字符的方式?
【问题讨论】:
-
你试过
A /= Angle(2);吗?