【发布时间】:2012-02-21 02:52:27
【问题描述】:
我对下面的逻辑有一些问题。我正在学习 unittest 模块并遇到了这段代码。
def matches(self, date):
return ((self.year and self.year == date.year or True) and
(self.month and self.month == date.month or True) and
(self.day and self.day == date.day or True) and
(self.weekday and self.weekday == date.weekday() or True))
在我看来,它总是会以 True 结束。在讨论为什么代码不起作用时,讨论了这种差异:
>>> c=1
>>> c and c == 2 or True
True
>>> c and c == (2 or True)
False
“c and c == 2 or True”与“c and c == (2 or True)”的逻辑是什么
我知道“==”的绑定比 or 强,但我不明白整个构造试图做什么。它用于启用通配符。作为一部分,我想我需要解释如何处理数字以及如何处理数字(我一直认为它与真/假条件有关。
这两个表达式的“c 和 c”部分的意义何在?
谢谢,
纳尼
【问题讨论】:
-
这看起来像非常可怕的代码 TBH。你从哪里弄来的?