function adjustment (python)


Following is a code fragment from bitbake script in BitBake project.
This fragment shows how to make some small adjustments to an existing function without changing its interface.
_warnings_showwarning = warnings.showwarning
def _showwarning(message, category, filename, lineno, file=None, line=None):
    if file is not None:
        if _warnings_showwarning is not None:
            _warnings_showwarning(message, category, filename, lineno, file, line)
    else:
        s = warnings.formatwarning(message, category, filename, lineno)
        warnlog.warn(s)

warnings.showwarning = _showwarning

Here's another ugly demo.
def func(num): 
    print num
    return

def runtest():
    func(1)
    func(2)
    func(3)
    func(4)
    return

runtest()

_old_func = func

def _new_func(num):
    if (num%2 == 0):
        _old_func(num)
    else:
        print "odd"
    return
    
func = _new_func

runtest()