Search code examples
pythonsetattr

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


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_*?


Solution

  • It's called delattr and is documented here.