I need to return the contents of a file, is it fine to do this :
def foo(filePath):
with open(filePath) as f:
return json.load(f)
Or should I do this instead :
def foo(filePath):
with open(filePath) as f:
r = json.load(f)
return r
(Of course my functions do other stuff, this is a toy model)
Returning inside a with block is not dangerous. Write it the first way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With