Python evolution: Unease

Michael Hobbs mike at hobbshouse.org
Tue Jan 4 11:02:07 EST 2005


Ville Vainio <ville at spammers.com> wrote:
> What form of extreme dynamic behaviour have you been using lately?

One real-world example: in my new coverage analysis tool (to be 
released any month now), I need to trace threads without changing any
code. To do so, I redefine the thread.start_new_thread() function from
outside the thread module, like so:


orig_start_new_thread = thread.start_new_thread

def traced_start_new_thread(func, args, kwargs={}):
    return orig_start_new_thread(traced_func_call, (func, args, kwargs))

def traced_func_call(func, args, kwargs):
    sys.settrace(my_trace_func)
    func(*args, **kwargs)

thread.start_new_thread = traced_start_new_thread


Granted, doing something like this on a regular basis is really bad
coding style, but the ability to do something like this when I really
need it is what I love about Python.




More information about the Python-list mailing list