Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Function

Tags:

python

I'm trying to create a function to delete another function.

 def delete_function(func):
    del func

is what I have so far, but for some reason it doesn't work.

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

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

del(foo)

but I'm trying to do it a different way. How?

like image 794
Bobby Tables Avatar asked Apr 28 '12 20:04

Bobby Tables


People also ask

What is delete function C++?

Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression. Delete can be used by either using Delete operator or Delete [ ] operator. New operator is used for dynamic memory allocation which puts variables on heap memory.

Is delete a function in Python?

Definition and UsageThe del keyword is used to delete objects. In Python everything is an object, so the del keyword can also be used to delete variables, lists, or parts of a list etc.

What is the of delete?

Delete means to erase. Delete has its roots in Latin and was first used to mean destroy. In modern usage, delete means to remove completely. Delete used in writing means to edit by removing, often done by drawing a line through the text to be deleted .

Which function is used to delete the objects?

The delete operator removes a given property from an object. On successful deletion, it will return true , else false will be returned.


1 Answers

Since foo is a global, you can delete it from the global definitions:

def delete_func(func):
    del globals()[func.func_name]
like image 142
Omri Barel Avatar answered Oct 13 '22 10:10

Omri Barel