Home > front end >  Can I overload -> in python?
Can I overload -> in python?

Time:07-14

I want to build a logic variable class that can do the following

x = LogicVar()
y = LogicVar()

property = x -> y

then this will give...

property(true, true) = true
property(true, false) = false
property(false, true) = true
property(false, false) = true

because I would be ideally be able to do something like

class LogicVar():
   ...

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

I know I can overload stuff like or >=, but I don't know whether I can do -> even though I see it used in type hints.

CodePudding user response:

No. -> is not an operator at all in Python. The only place a -> token is permitted in the Python grammar is before the return type annotation in a function definition. Python operator overloading does not allow you to change the language syntax or create new operators.

  • Related