Im trying to understand how to create a custom print function. (using python 2.7)
import sys
class CustomPrint():
    def __init__(self):
        self.old_stdout=sys.stdout #save stdout
    def write(self, text):
        sys.stdout = self.old_stdout #restore normal stdout and print
        print 'custom Print--->' + text
        sys.stdout= self # make stdout use CustomPrint on next 'print'
                         # this is the line that trigers the problem
                         # how to avoid this??
myPrint = CustomPrint()
sys.stdout = myPrint
print 'why you make 2 lines??...'
The code above prints this to console:
>>> 
custom Print--->why you make 2 lines??...
custom Print--->
>>> 
and i want to print only one line:
>>>    
1custom Print--->why you make 2 lines??...
>>>
But cant figure out how to make this custom print work , i understand that there's some kind of recursion that triggers the second output to the console (i use self.write , to assign stdout to self.write himself !)
how can i make this work ? or is my approach just completely wrong...
 
     
     
     
    