Python-如何从类中动态删除方法-即setattr的对面

Python-如何从类中动态删除方法-即setattr的对面

问题描述:

我不知道这里是否有一个好的设计,但是我有一个派生自unittest.TestCase的类及其设置方式,我的代码将动态地将一堆test_*方法注入调用unittest进行运行之前的类.我为此使用setattr.这一直很好,但是现在我要删除以前注入的方法,然后注入一组新的方法.如何删除名称与模式test_*匹配的类中的所有方法?

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

它称为delattr,并记录在这里.