UCB CS61A HW01 Q5: If Function vs Statement

def cond():
	return False

def true_func():
	print(42)

def false_func():
	print(47)

If Statement

1 def with_if_statement():
2 	  if cond():
3 		  return true_func()
4 	  else:
5		  return false_func()
6     result = with_if_statement() # 47
7     print(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 Framewith_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 Framewith_if_statement
with_if_statement·–> func with_if_statement()
Local Framewith_if_statement
  • Step 3 【2】[6]
  • Step 4 【5】[2]
Global Framewith_if_statement
with_if_statement·–> func with_if_statement()
Local Framewith_if_statement
Return valueNone ·->print(47)
  • Step 【6】[5]
>>> result = with_if_statement()
47
Global Framewith_if_statement
with_if_statement·–> func with_if_statement()
resultNone ·->print(47)
Local Framewith_if_statement
Return valueNone ·->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.

If Function

1 def if_function(condition, true_result, false_resule):
2	  if condition:
3		  return true_result
4	  else:
5		  return false_result

6 def with_if_function()
7     return if_function(cond(), true_func(), false_func())
8 result = with_if_function()
9 print(result)

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 Framewith_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).
Global Frame
if_functiont·–> func if_function()
with_if_function·–> func with_if_function()
Local Framewith_if_function
with_if_functiont·–> func with_if_function()
Local Framewith_if_function
with_if_functiont·–> func if_function()
func()0 ·–> False
true_func()None ·–> print(42)
false_func()None ·–> print(47)
  • Step 5 【2】[7]
  • Step 6 【5】[2]
Global Frame
if_functiont·–> func if_function()
with_if_function·–> func with_if_function()
Local Framewith_if_function
with_if_functiont·–> func with_if_function()
Local Frameif_function
with_if_functiont·–> func if_function()
func()0 ·–> False
true_func()None ·–> print(42)
false_func()None ·–> print(47)
Return valueNone ·–> print(47)
  • Step 7 【7】[5]
Global Frame
if_functiont·–> func if_function()
with_if_function·–> func with_if_function()
Local Framewith_if_function
with_if_functiont·–> func with_if_function()
Return valueNone ·–> print(47)
Local Frameif_function
with_if_functiont·–> func if_function()
func()0 ·–> False
true_func()None ·–> print(42)
false_func()None ·–> print(47)
Return valueNone ·–> print(47)
  • Step 8 【8】[7]
>>> result = with_if_statement()
42
47
Global Frame
if_functiont·–> func if_function()
with_if_function·–> func with_if_function()
resultNone ·–> print(47)
Local Framewith_if_function
with_if_functiont·–> func with_if_function()
Return valueNone ·–> print(47)
Local Frameif_function
with_if_functiont·–> func if_function()
func()0 ·–> False
true_func()None ·–> print(42)
false_func()None ·–> print(47)
Return valueNone ·–> print(47)
  • Step 9 【9】[8]
>>> print(result) # print(None)
None
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值