Is `returning .. return` unreachable code?

PyCharm warns about this code, stating that the last return is not available:

def foo():
    with open(...):
        return 1
    return 0

I expect the second refund to be completed if open()failed. Who is right?

+4
source share
1 answer

PyCharm is right. If open()not satisfied, an exception occurs and is not reached return.

with , . , with , __exit__ __enter__. , , . with .

+11

Source: https://habr.com/ru/post/1664528/


All Articles