Is returning inside a 'with' statement dangerous?

Viewed 649

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)

1 Answers

Returning inside a with block is not dangerous. Write it the first way.

Related