码迷,mamicode.com
首页 > 编程语言 > 详细

If function in python

时间:2015-09-01 09:12:06      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

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

 

If function in python

标签:

原文地址:http://www.cnblogs.com/ch3cooh/p/if_function.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!