acidzombie24 acidzombie24 - 4 months ago 30
Python Question

overload print python

Am i able to overload the print function? and call the normal function? What i want to do is after a specific line i want print to call my print which will call the normal print and write a copy to file.

Also i dont know how to overload print. I dont know how to do variable length arguments. i'll look it up soon but http://stackoverflow.com/questions/550470/overload-print-python/550477#550477 just told me i cant overload print in 2.x which is what i am using.

Answer

Overloading print is a design feature of python 3.0 to address your lack of ability to do so in python 2.x.

However, you can override sys.stdout. (example.) Just assign it to another file-like object that does what you want.

Alternatively, you could just pipe your script through the the unix tee command. python yourscript.py | tee output.txt will print to both stdout and to output.txt, but this will capture all output.

Comments