1defwith_if_statement():2if cond():3return true_func()4else:5return false_func()6 result = with_if_statement()# 477print(result)# None
line that has just executed: [] next line to execute: 【】
Step 1 【1】 First, the definition statement for the function with_if_statement is executed. Notice that the entire def statement is processed in a single step. The body of a function is not executed until the function called (not when it is defined). After executing the first definition statement, only the name with_if_statement if bound in the global frame.
Global Frame
with_if_statement
with_if_statement
·–> func with_if_statement
Step 2 【6】[1] Next, the with_if_statement function is called, and so a new frame is created.
Global Frame
with_if_statement
with_if_statement
·–> func with_if_statement()
Local Frame
with_if_statement
Step 3 【2】[6]
Step 4 【5】[2]
Global Frame
with_if_statement
with_if_statement
·–> func with_if_statement()
Local Frame
with_if_statement
Return value
None ·->print(47)
Step 【6】[5]
>>> result = with_if_statement()47
Global Frame
with_if_statement
with_if_statement
·–> func with_if_statement()
result
None ·->print(47)
Local Frame
with_if_statement
Return value
None ·->print(47)
Step 【7】[6]
>>>print(result)# print(None)None
The value that print returns is always None, a special Python value that represents nothing. The interactive Python interpreter does not automatically print the value None. In the case of print, the function itself is printing output as a side effect of being called.
line that has just executed: [] next line to execute: 【】
Step 1 【1】
Global Frame
if_functiont
·–> func if_function()
Step 2 【6】[1]
Global Frame
if_functiont
·–> func if_function()
with_if_function
·–> func with_if_function()
Step 3 【8】[6]
Global Frame
if_functiont
·–> func if_function()
with_if_function
·–> func with_if_function()
Local Frame
with_if_function
with_if_functiont
·–> func with_if_function()
Step 4 【7】[8] Next, the if_function() function is called, and so a new frame is created with the formal parameters, cond() bound to the value of False, true_func() bound to the value of print(42), and false_func() bound to the value of print(47).