def hook?

Roy Smith roy at panix.com
Wed Nov 20 18:57:00 EST 2002


I'd like to have a bit of code run every time a function is defined.
The original reason for this was that I was writing a unit test
framework, and wanted a way to register my test case methods
automatically as they got defined.  I've since discovered PyUnit, and
understand the introspection magic it does to get around this, but
it's still an interesting problem in isolation.

If Python were more lisp-like, there would be nothing magic about def;
it would just take a function body and a name and do an assignment.  I
could imagine doing something like:

-----begin theoretical quasi-python code------

functionNameList = []

def myDef (name, body):
    name = body
    functionNameList.append (name)

myDef myFunc:
      print "this is my function"

for func in functionNameList:
    func()
----------------------------------------------

Is there anyway to do anything like that?



More information about the Python-list mailing list