Home > Software design >  Is it possible to overload <> in python
Is it possible to overload <> in python

Time:11-07

I just came to know that you can overload operators in python using __add__ or __sub__ etc. Even [] can be overloaded with __getitem__. So it is possibe to overload <> like load_text<"file.txt">.

CodePudding user response:

You can't overload an operator that doesn't exist, and since Python doesn't have a bracketed call operator, you can't overload it. Sometimes you can do tricks like overloading < to return an object that has a > overload, but it won't work in this case because > is a binary operator, and you have nothing to put on the right side.

Python does have a regular call operator that you can overload with __call__ to get you load_text("file.txt").

  • Related