Re: Misleading wikipedia article on Python 3?
- From: "Evan Klitzke" <evan@xxxxxxxx>
- Date: Wed, 8 Aug 2007 21:51:21 -0700
On 8/8/07, greg <greg@xxxxxxxxxxxxxxxxxxxxx> wrote:
Istvan Albert wrote:
A solution would be writing the code with a logging function to begin
with, alas many times that is out of one's hand.
If the code has been written with calls to a builtin
print function, the situation isn't much better. You
could monkeypatch the print function, but that's
probably worse than swapping sys.stdout.
You can easily modify print in a safe way. Here's an example, that
will work in any recent version of Python:
import sys
def print_(s):
print s
def logger(method):
def wrapper(s):
sys.stderr.write('Logging: %s\n' % s)
method(s)
return wrapper
print_ = logger(print_)
print_('hello')
Running this code will do a regular print of 'hello', as well as
"logging" it to stderr. As a function, you can convert print wholesale
to another logging function, or wrap it transparently like this to
provide additional logging functionality. What do you find wrong with
this sort of "monkeypatching"?
--
Evan Klitzke <evan@xxxxxxxx>
.
- Follow-Ups:
- References:
- Misleading wikipedia article on Python 3?
- From: John J. Lee
- Re: Misleading wikipedia article on Python 3?
- From: "Martin v. Löwis"
- Re: Misleading wikipedia article on Python 3?
- From: John J. Lee
- Re: Misleading wikipedia article on Python 3?
- From: "Martin v. Löwis"
- Re: Misleading wikipedia article on Python 3?
- From: John J. Lee
- Re: Misleading wikipedia article on Python 3?
- From: Carsten Haese
- Re: Misleading wikipedia article on Python 3?
- From: Paul Rubin
- Re: Misleading wikipedia article on Python 3?
- From: Istvan Albert
- Re: Misleading wikipedia article on Python 3?
- From: greg
- Misleading wikipedia article on Python 3?
- Prev by Date: Re: How can I programmatically find the name of a method from within that method?
- Next by Date: wxPython before MainLoop
- Previous by thread: Re: Misleading wikipedia article on Python 3?
- Next by thread: Re: Misleading wikipedia article on Python 3?
- Index(es):
Relevant Pages
|