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.
Definition and Usage The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.
To print all instances of a class with Python, we can use the gc module. We have the A class and we create 2 instances of it, which we assigned to a1 and a2 . Then we loop through the objects in memory with gc. get_objects with a for loop.
For those reviewing the previously dated answers, as of version release "Python 2.6" there is a new answer to the original poster's question.
In Python 2.6 and up, you can disable the print statement in favor of the print function, and then override the print function with your own print function:
from __future__ import print_function
# This must be the first statement before other statements.
# You may only put a quoted or triple quoted string,
# Python comments, other future statements, or blank lines before the __future__ line.
try:
import __builtin__
except ImportError:
# Python 3
import builtins as __builtin__
def print(*args, **kwargs):
"""My custom print() function."""
# Adding new arguments to the print function signature
# is probably a bad idea.
# Instead consider testing if custom argument keywords
# are present in kwargs
__builtin__.print('My overridden print() function!')
return __builtin__.print(*args, **kwargs)
Of course you'll need to consider that this print function is only module wide at this point. You could choose to override __builtin__.print
, but you'll need to save the original __builtin__.print
; likely mucking with the __builtin__
namespace.
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.
class MovieDesc:
name = "Name"
genders = "Genders"
country = "Country"
def __str__(self):
#Do whatever you want here
return "Name: {0}\tGenders: {1} Country: {2} ".format(self.name,self.genders,self.country)
)
I came across the same problem.
How about this:
class writer :
def __init__(self, *writers) :
self.writers = writers
def write(self, text) :
for w in self.writers :
w.write(text)
import sys
saved = sys.stdout
fout = file('out.log', 'w')
sys.stdout = writer(sys.stdout, fout)
print "There you go."
sys.stdout = saved
fout.close()
It worked like a charm for me. It was taken from http://mail.python.org/pipermail/python-list/2003-February/188788.html
In Python 2.x you can't, because print isn't a function, it's a statement. In Python 3 print is a function, so I suppose it could be overridden (haven't tried it, though).
Though you can't replace the print
keyword (in Python 2.x print
is a keyword), it's common practice to replace sys.stdout
to do something similar to print
overriding; for example, with an instance of StringIO.StringIO
. This will capture all of the printed data in the StringIO
instance, after which you can manipulate it.
I answered the same question on a different SO question
Essentially, simplest solution is to just redirect the output to stderr as follows, in the wsgi config file.
sys.stdout = sys.stderr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With