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 yield __builtins__.print = old_printI'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 gets it.
FWIW, it doesn't work, either. :-}
Right. Lie answered why. I didn't pay attention and thought you were already using Python 3.
-- Robert Kern "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 -- http://mail.python.org/mailman/listinfo/python-list
