在“with”语句中返回危险吗?

我需要返回文件的内容,这样做可以吗:


def foo(filePath):

    with open(filePath) as f:

        return json.load(f)

或者我应该这样做:


def foo(filePath):

    with open(filePath) as f:

        r = json.load(f)

    return r

(当然我的功能还可以做其他事情,这是一个玩具模型)


慕田峪9158850
浏览 55回答 1
1回答

潇潇雨雨

返回with区块内并不危险。按照第一种方式写。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python