0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[短文ネタ] try節、except節とfinally節の両方でreturnした場合

Posted at

ふと気になったので、やってみた。Python2.7とPython3.4で確認。

try節とfinally節でreturn
def f():
    try:
        return "try"
    finally:
        return "finally"

print(f())

finally

except節とfinally節でreturn
def f():
    try:
        raise ValueError
    except ValueError:
        return "except"
    finally:
        return "finally"

print(f())

finally

たぶん使うことはないけど。知らなかった。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?