标签:
We can easily define a if_function which seemingly does exactly what the if statement does. However, this is not the entire story.
1 ## if_function is not the same as the built-in if statement 2 3 def if_function(cond, true_result, false_result): 4 if cond: 5 return true_result 6 else: 7 return false_result 8 9 flag = 0 10 11 def cond(): 12 return flag 13 14 def true_statement(): 15 global flag 16 flag += 1 17 return flag 18 19 def false_statement(): 20 global flag 21 flag += 1 22 return flag 23 24 def using_if_statement(): 25 if cond(): 26 return true_statement() 27 else: 28 return false_statement() 29 30 def using_if_function(): 31 return if_function(cond(), true_statement(), false_statement()) 32 33 print using_if_statement() 34 print using_if_function() 35 36 ## Output 37 ## 1 38 ## 2
标签:
原文地址:http://www.cnblogs.com/ch3cooh/p/if_function.html