what is the problem you hope to solve with this technique?
–
IfLoopApr 28 '12 at 20:56

1

Indeed. Why, for the love of the code, why?
–
MackeApr 28 '12 at 21:04

If you have to do this, there's probably something wrong with your implementation.
–
Joel CornettApr 28 '12 at 21:52

Joe Loo, if one of the answers you've been given actually answers your question -- mine or glglgl's saying "no, don't do this", or Omri's, giving a way to do it in some cases but that may do the Wrong Thing in others -- then you should accept whatever answer answers your question best. If not, you should edit your question to make it clearer what you think still needs answering.
–
Gareth McCaughanMay 2 '12 at 9:08

3 Answers
3

Deleting a function isn't really something you do to the function itself; it's something you do to the namespace it lives in. (Just as removing the number 3 from a list isn't something you do to the number 3, it's something you do to the list.)

Suppose you say

def foo(x): return 1
bar = foo

Then (more or less) you have two names, foo and bar, for the exact same function. Now suppose you call delete_function(foo) or delete_function(bar). The exact same thing, namely a function object, is being passed to delete_function. But what you actually want to remove is the association between the name foo or bar and that object -- and there's no possible way delete_function (however you define it) can know whether it's foo or bar or something else you're wanting to get rid of.

(Well ... Actually, there is. There are nasty hacky things you can do that would let the code in delete_function know more about how it was called. But don't even think about thinking about them.)

So. Your options are as follows. (1) Nasty hacky things, as just mentioned. Don't. (2) Pass delete_function not the function object but information about the name of the function and the thing you're trying to delete it from. This is ugly and ungainly. (3) Don't bother.

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...