在 'with' 语句中返回危险吗?
Is returning inside a 'with' statement dangerous?
我需要return一个文件的内容,这样可以吗:
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
(当然我的函数做其他的东西,这是一个玩具模型)
在 with
块内返回并不危险。写第一种吧
我需要return一个文件的内容,这样可以吗:
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
(当然我的函数做其他的东西,这是一个玩具模型)
在 with
块内返回并不危险。写第一种吧