Removing a function is not what you do with the function itself; this is what you do with the namespace in which he lives. (Just like removing number 3 from a list is not what you do with number 3, it is what you do with this list.)
Suppose you say
def foo(x): return 1 bar = foo
Then (more or less) you have two names, foo and bar , for the same function. Now suppose you call delete_function(foo) or delete_function(bar) . Exactly the same thing, namely the function object, is passed to delete_function . But what you really want to delete is the relationship between the name foo or bar and this object - and there is no possible way delete_function (no matter how you define it) can know if it is foo or bar or something else, what do you want to get rid of.
(Well ... Actually, there is. There are unpleasant hacking things that you can do to make 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 hacking stuff, as just mentioned. Not. (2) Pass delete_function not the function object, but information about the name of the function and what you are trying to delete. This is ugly and awkward. (3) Do not worry.
I highly recommend # 3, unless the only reason you do this is to learn more about how Python works. In the latter case, http://docs.python.org/reference/executionmodel.html might be a good place to start.
Gareth mccaughan
source share