Delete function

I am trying to create a function to delete another function.

def delete_function(func): del func 

- this is what I still have, but for some reason it does not work.

 def foo(): print("foo") delete_function(foo) 

doesn't seem to do the trick. I know you can do it easily, just

 del(foo) 

but I try to do it differently. How?

+8
python
source share
3 answers

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.

+13
source share

Since foo is global, you can remove it from global definitions:

 def delete_func(func): del globals()[func.func_name] 
+7
source share

It just won't work.

What you are trying to do is essentially the namespace of the caller.

Try the following:

 print "0", locals() def foo(): pass print "1", locals() del foo print "2", locals() 

note that

  • locals dict at 0 and 2 are identical, and at 1 almost - except that it has the additional purpose of foo .
  • del is an operator, not a function

If you execute del in the delete_function() function, essentially the deletion inside your function is deleted (which is useless because the function is terminated immediately), while the caller does the job.

Strictly speaking, del does not delete objects, but simply assigns names to objects. Objects are deleted β€œautomatically” (garbage collection) as soon as they are no longer referenced.

MAY WORK what you are trying to do by checking the stack frame and passing in a name that will be deleted as a string, but that will be PITA.

Maybe you will try

 del locals()['foo'] 

or

 locals()['foo'] = 42 

? But I think that this does not guarantee that it really modifies the real locals dictionary, it can also work with a copy and, thus, remain without effects ...

+4
source share

All Articles