python How to call ob1.fun1.fun2 and handle ob1.fun1 as none?
<p>I tried to use the following code, but looks like ugly</p>

<pre><code>tmp = ob1.fun1
result = None
if tmp is not None:
   global result
   result = tmp.fun2
</code></pre>

<p>Is there a better way to do this?</p>

<p>If you just want <code>result</code> to be <code>None</code> if <code>ob1.fun1</code> is <code>None</code> or if <code>fun2</code> doesn't exist as an attribute, you could use <a href="https://docs.python.org/3/library/functions.html#getattr" rel="nofollow"><code>getattr</code></a> and use <code>None</code> as a default. Note that <code>getattr(None, 'attr', something)</code> will return <code>something</code>.</p>

<pre><code>result = getattr(ob1.fun1, 'fun2', None)
</code></pre>

python