相反使用if / else测试变量或函数,使用如下所述的try / exception会更加pythonic?
def fufu(oi):
try:
int(oi)
except:
raise
else:
return True
some_var = 1
try:
some_var
except NameError:
print("some_far not defined, boo hoo")
else:
try:
fufu(some_var)
except:
print("i'm not a guitarr, so i don't accept string,")
else:
print("Thank you for the integer: {}".format(some_var))
finally:
print("we're done")
finally:
print("test finished")