-1

Would it be possible to overload '>' as an operator on two strings. This would be nice as it would allow for custom results while debugging. In my case, I could also edit the source code and add prints, etc., but the Monkey Patch is quicker and available without a rebuild.

The desired result would be that typing (for example):

>>>import os
>>>"string1" > "string2"

would call some custom code that I've created. Other options might be to use decorators, or some of the solutions here:

Redirect stdout to a file in Python?

user3761340
  • 603
  • 5
  • 19
  • 2
    Python isn't a shell. If you want a shell, use a shell. Trying to turn Python into a crappy shell substitute is going to get you worse results than using a shell normally or using Python normally. – user2357112 Mar 18 '22 at 16:29
  • 4
    Do you *really* want to completely break string comparison just to get a slightly more convenient syntax for writing to files? – user2357112 Mar 18 '22 at 16:30
  • 1
    You cannot overload operators on built-in types. – md2perpe Mar 18 '22 at 16:55
  • If you want a language that encourages this kind of monkeypatching, Ruby is not hard to find. [The Python Way](https://peps.python.org/pep-0020/) strongly discourages things that happen behind your back and spooky action at a distance, because Python's philosophy values predictability, understandability, readability, etc. This kind of hack is very much contrary to "explicit is better than implicit", _and_ to "there should be one, and only one, obvious way to do it". It's also contrary to "special cases aren't special enough to break the rules". – Charles Duffy Mar 18 '22 at 17:17
  • (A language that's _even more_ attached to having optional special cases / several ways to do things in its syntax is Perl, and the resulting difficulty in maintaining other peoples' perl has a great deal to do with why folks flocked away from it to Python when the latter emerged as a practical alternative). – Charles Duffy Mar 18 '22 at 17:20
  • I do very much appreciate the vast concern. Please let it be known that the risks in such endeavors are well-understood. The question is intended in a Beazlyian context. (See any of many PyCon presentations from David Beazley for context.) – user3761340 Mar 18 '22 at 23:52
  • Here is a good one from David, for example: https://www.youtube.com/watch?v=sPiWg5jSoZI – user3761340 Mar 19 '22 at 00:24
  • I've modified the question to address everyone's concerns. – user3761340 Mar 19 '22 at 20:42
  • I've further modified the question based on feedback. – user3761340 Mar 20 '22 at 18:39

1 Answers1

1

If you derive a class from str I guess you could:

class pipe_string(str):
    def __gt__(self, other):
        with open(other, "w") as file:
            file.write(self)

pipe_string("foo") > "bar.txt"

But I would discourage it I think. Might be better to send whatever you are looking to send to stdout via python and use your shell to do shell things.

JonSG
  • 10,542
  • 2
  • 25
  • 36