擅长:python、mysql、java
<p><code>delattr()</code>就是你想要的。循环遍历类的<code>vars()</code>,并测试以<code>"test_"</code>开头的属性名。E、 g</p>
<pre><code>@classmethod
def remove_test_methods(cls):
for name in list(vars(cls)):
if name.startswith("test_") and callable(getattr(cls, name)):
delattr(cls, name)
</code></pre>
<p>我建议不要使用<code>dir()</code>,因为这也会显示来自父类的名称,所以并非所有从<code>dir()</code>获得的名称都可以在目标类上定义。</p>