try except else finally

it2023-02-04  38

python的try finally (还真不简单)

def f(): try: print 1 return 1 finally: print 0 return 0

结果为 1 0 0 无论try里执行什么,即使是return,也会调用finally的

def f(): try: print 1 return 1 except: return 2 else: print 3 return 3 finally: print 0 #return 0

结果为 1 0 1 try里面最后的return,阻挡了else的执行,但是并没有影响finally的执行

最新回复(0)