我可以在 python 中重载 -> 吗?

use*_*659 2 python python-3.x

我想构建一个可以执行以下操作的逻辑变量类

x = LogicVar()
y = LogicVar()

property = x -> y
Run Code Online (Sandbox Code Playgroud)

那么这将给...

property(true, true) = true
property(true, false) = false
property(false, true) = true
property(false, false) = true
Run Code Online (Sandbox Code Playgroud)

因为我理想情况下能够做类似的事情

class LogicVar():
   ...

   def __dash_arrow_(self, other):
      def ifthen(x, y):
         return (not(x) or y)
      return ifthen

Run Code Online (Sandbox Code Playgroud)

我知道我可以重载像 + 或 >= 这样的东西,但我不知道我是否可以这样做 -> 即使我看到它在类型提示中使用。

use*_*ica 5

No.->在 Python 中根本不是运算符。Python 语法中唯一->允许使用标记的位置是函数定义中的返回类型注释之前。Python 运算符重载不允许您更改语言语法或创建新运算符。

  • 可重写运算符的完整列表位于[此处](https://docs.python.org/3/reference/datamodel.html),主要位于[模拟数字类型](https://docs.python.org /3/reference/datamodel.html#emulated-numeric-types)。如果它不在该页面上,则不可能。 (2认同)