Re: Misleading wikipedia article on Python 3?
- From: Ricardo Aráoz <ricaraoz@xxxxxxxxx>
- Date: Thu, 09 Aug 2007 14:06:50 -0300
Evan Klitzke wrote:
On 8/8/07, greg <greg@xxxxxxxxxxxxxxxxxxxxx> wrote:
Istvan Albert wrote:
A solution would be writing the code with a logging function to beginIf the code has been written with calls to a builtin
with, alas many times that is out of one's hand.
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"?
foolish question maybe.
Why wouldn't you do it this way ?
def print_(s):
print s
def logger(m):
sys.stderr.write('Logging: %s\n' % m)
method(m)
print_ = logger(print_)
print_('hello')
.
- 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 to get command output using python
- Next by Date: Re: Stackless Integration
- Previous by thread: Re: Misleading wikipedia article on Python 3?
- Next by thread: Re: Misleading wikipedia article on Python 3?
- Index(es):
Relevant Pages
|