I tried to use the following code, but looks like ugly
tmp = ob1.fun1
result = None
if tmp is not None:
global result
result = tmp.fun2
Is there a better way to do this?
Use the EAFP (Easier to ask for forgiveness than permission) approach. Wrap it in a try/except and handle your exception accordingly:
result = None
try:
result = ob1.fun1.fun2
except AttributeError:
# do your exception handling here
You could also use hasattr to check if fun1
is in ob1
result = None
if hasattr(ob1, 'fun1'):
res = ob1.fun1.fun2
else:
print('no fun1 found in ob1')
# or raise an Exception