Skip to main content
 首页 » 编程设计

Python对比评测

2025年05月04日9yjmyzz

根据 python 文档,x<y<z比较翻译成x<y and y<z和表达式 y最多只被评估一次。
现在我的问题是,表达式 y (看下面的代码)这里只求值一次?

if(x<y and y<z): 

请您参考如下方法:

两次:

>>> def f(): 
...   print "F called" 
...   return 1 
... 
>>> 0 < f() < 100 
F called 
True 
>>> 0 < f() and f() < 100 
F called 
F called 
True 
>>> if (0 < f() and f() < 100): 
...   print True 
... 
F called 
F called 
True 
>>>