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?
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.
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.
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 .
The delete operator removes a given property from an object. On successful deletion, it will return true , else false will be returned.
Since foo
is a global, you can delete it from the global definitions:
def delete_func(func):
del globals()[func.func_name]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With