Globally override built-in print function?
robert.kern at gmail.com
Fri Apr 16 17:20:21 CEST 2010
On 2010-04-15 21:17 PM, Dave W. wrote:
>>> I naively thought I could capture output from exec()'ed print
>>> invocations by (somehow) overriding 'print' globally. But this
>>> seems not to be possible.<snip>
>> old_print = __builtins__.print
>> __builtins__.print = printhook
>> __builtins__.print = old_print
> I'm pretty sure this is semantically equivalent to my original code,
> but I gave it a try anyway.
Not at all. Declaring "global print" then assigning to "print" simply changes
what the module's "print" variable refers to. Other modules are unaffected.
"Global" variables aren't truly global; they are actually local to the module.
You need to replace it in the __builtins__ because that's where everyone else
> FWIW, it doesn't work, either. :-}
Right. Lie answered why. I didn't pay attention and thought you were already
using Python 3.
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list