<分区>
我需要返回一个文件的内容,这样做可以吗:
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
(当然我的功能还做其他事情,这是一个玩具模型)
标签 python python-3.8
<分区>
我需要返回一个文件的内容,这样做可以吗:
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
block 内返回并不危险。第一种写法。
关于python - 在 'with' 语句中返回危险吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64469248/
相关文章:
python - 如何编码照片并将其与网络摄像头输入进行比较?
python - "pyenv versions"和 "python -V"显示不同的版本
python - 如何在 python 中的文件中的新行上写入输入,而不添加空行?
python - 如何从具有反向功能的 ModelViewSet(Django Rest Framework)获取操作方法的 url?
python ,opencv : duration of time an object is detected on webcam
python - Msys2 与 python 3.8 : ImportError: cannot import name 'open_code' from 'io'