擅长:python、mysql、java
<p>您可以获取实例的所有“public”方法的列表:</p>
<pre><code>x = Foo('hi','bye')
public_method_names = [method for method in dir(x) if callable(getattr(x, method)) if not method.startswith('_')] # 'private' methods start from _
for method in public_method_names:
getattr(x, method)() # call
</code></pre>
<p>有关<a href="https://docs.python.org/3/library/functions.html#getattr" rel="nofollow noreferrer">getattr</a>的详细信息
实际上,Python没有<code>public</code>或<code>private</code>语义,如果感兴趣,可以读取<a href="https://stackoverflow.com/questions/1301346/the-meaning-of-a-single-and-a-double-underscore-before-an-object-name-in-python">that</a></p>