可爱的 Python: Python 中的函数编程
2007-03-29 12:05:55 来源:WEB开发网核心提示: 清单 1. Python 中的“短路”条件调用# Normal statement-based flow controlif <cond1>: func1()elif <cond2>: func2()else: func3()# Equiva
清单 1. Python 中的“短路”条件调用
# Normal statement-based flow control
if
<cond1>: func1()
elif
<cond2>: func2()
else
: func3()
# Equivalent "short circuit" expression
(<cond1>
and
func1())
or
(<cond2>
and
func2())
or
(func3())
# Example "short circuit" expression
>>> x = 3
>>>
def
pr
(s):
return
s
>>> (x==1
and
pr(
'one'))
or
(x==2
and
pr(
'two'))
or
(pr(
'other'))
'other'
>>> x = 2
>>> (x==1
and
pr(
'one'))
or
(x==2
and
pr(
'two'))
or
(pr(
'other'))
'two'
更多精彩
赞助商链接