擅长:python、mysql、java
<p>是的,使用<code>getattr()</code>获取属性,使用<code>callable()</code>验证它是一个方法:</p>
<pre><code>invert_op = getattr(self, "invert_op", None)
if callable(invert_op):
invert_op(self.path.parent_op)
</code></pre>
<p>注意,<code>getattr()</code>通常在属性不存在时抛出异常。但是,如果指定默认值(<code>None</code>,在本例中),它将返回该值。</p>