Python-如何从类中动态删除方法-即setattr的对面 [英] Python - how can I dynamically remove a method from a class -- i.e. opposite of setattr

查看:125
本文介绍了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,并记录在这里.

这篇关于Python-如何从类中动态删除方法-即setattr的对面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆