It just won't work.
What you are trying to do is, essentially, the name space of the caller.
Try this:
print "0", locals()
def foo(): pass
print "1", locals()
del foo
print "2", locals()
Note that
- the locals dict at 0 and 2 are identical, and at 1 nearly - except it has the additional assignment of
foo.
del is a statement and not a function
If you do the del in a function delete_function(), essentially the assignment within your function gets removed (which is effectless because the function is terminated immediately), while the caller keeps the assignment.
Strictly spoken, del does not delete objects, but merely assignments from names to objects. Objects get deleted "automatically" (garbage collected) as soon as they are not referenced any longer.
It COULD work what you try to do by inspection of the stack frame and passing of the name to be deleted as a string, but it would be a PITA.
Maybe you try
del locals()['foo']
or
locals()['foo'] = 42
? But I think it is not guarantteed that it really modifies the real locals dictionary, it might as well operate on a copy and thus stay effectless...