Pop return from stack?

Dave Angel davea at ieee.org
Sun Aug 15 14:30:54 CEST 2010


Steven D'Aprano wrote:
> On Sat, 14 Aug 2010 16:05:05 -0700, bvdp wrote:
>
>   
>> <snip>
>>
>> def error(s):
>>     print "Error", s
>>     sys.exit(1)
>> <snip>
>>     
> This general technique is called "monkey patching". 
>
>   
> <snip>
> You can either manually exit from your own error handler:
>
> def myerror(s):
>     print "new error message"
>     sys.exit(2)
>
>
> or call the original error handler:
>
>
> def myerror(s):
>     print "new error message"
>     foo._error(s)
>
>
> That second technique requires some preparation before hand. In module 
> foo, after defining the error() function, you then need to create a 
> second, private, name to it:
>
> _error = error
>
>
>   
>
Small point.  The OP's request was that he not modify the called module, 
which is why he was considering monkey-patching.  And you can readily 
avoid adding that line to the file.  Just do something like this:

import foo
_olderror_func = foo.error

def myerror(s)
      print "new error message"
      _olderror_func(s)

DaveA




More information about the Python-list mailing list