return statement inside a "with"

48 Views Asked by At

My basic understanding of the "with"-idiom is this:

Calling

with obj:
  further code.. inside with
#endwith
code after if block

means that after the with-block ended, the __exit__ method on obj will be called. However, assume I have the with-call within a function definition, and inside it, the code has the following structure:

def fct():
  with obj:
     return call_other_fct(params)

Now my question: When does the __exit__ run?

  1. After the execution of the return statement
  2. Before the execution of the return statement but after the call to call_other_fct
  3. It does not run at all
0

There are 0 best solutions below