Python - how can I dynamically remove a method from a class -- i.e. opposite of setattr

jononomo picture jononomo · May 22, 2013 · Viewed 14.8k times · Source

I don't know if I have a good design here, but I have a class that is derived from unittest.TestCase and the way I have it set up, my code will dynamically inject a bunch of test_* methods into the class before invoking unittest to run through it. I use setattr for this. This has been working well, but now I have a situation in which I want to remove the methods I previously injected and inject a new set of methods. How can I remove all the methods in a class whose names match the pattern test_*?

Answer

BrenBarn picture BrenBarn · May 22, 2013

It's called delattr and is documented here.