I’m having some problems with the logic below. I was learning about the unittest module and came across this code.
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))
Which to me looks like it will always end up True. In discussing why the code doesn’t work, this difference was discussed:
>>> c=1
>>> c and c == 2 or True
True
>>> c and c == (2 or True)
False
What is the logic for either of “c and c == 2 or True” vs “c and c == (2 or True)”
I known that “==” binds stronger than or, but I don’t understand what the entire construct is trying to do. It being used to enable a wildcard. As a part, I guess I need explanation on how and works on numbers (I always thought about it in relation to True/False conditions.
What is the point of the “c and c” part of either expression?
Thanks,
Narnie
Actually, I figured out what the author was trying to do.
The author likes python, but comes from a C background. He was trying to simulate a ternary operation as in:
The pythonic way of doing this is not
stuff, but to use this as of python 2.5 and up:
If using a lower version of python, do: