This is not recursion. It happens that your write function is called twice, once with the text you expect, the second time only with '\n' . Try the following:
import sys class CustomPrint(): def __init__(self): self.old_stdout=sys.stdout def write(self, text): text = text.rstrip() if len(text) == 0: return self.old_stdout.write('custom Print--->' + text + '\n')
What I do in the above code, I add a new line character to the text passed in the first call, and make sure that the second call made by the print statement, which is designed to print a new line, does not print anything.
Now try to comment on the first two lines and see what happens:
def write(self, text):
source share