经典代码(同样适用于Java、JavaScript):
def lol(): try: return True finally: return False
调用lol()
后,究竟是返回False
还是True
呢?相信你和我一样,最开始认为是True
,因为try
中的语句先执行,对吧?
但事实上:
>>> lol()
False
try
had a try. Finally, finally
won.
所以,不要在异常语句中用return……
经典代码(同样适用于Java、JavaScript):
def lol(): try: return True finally: return False
调用lol()
后,究竟是返回False
还是True
呢?相信你和我一样,最开始认为是True
,因为try
中的语句先执行,对吧?
但事实上:
>>> lol()
False
try
had a try. Finally, finally
won.
所以,不要在异常语句中用return……
| Theme: Aeros 2.0 by TheBuckmaker.com