2 ms·
It isn't a type error: % python Python 3.6.0 ... >>> answer = "yes" >>> if answer == "y" or "yes": ... print("Thanks") ... Thanks The expres
by eesmith 9y ago
It isn't a type error:
% python
Python 3.6.0 ...
>>> answer = "yes"
>>> if answer == "y" or "yes":
... print("Thanks")
...
Thanks
The expression is the same as: (answer == "y") or "yes"
If answer is 'y' then the if-expression evaluates to True; for any other string it evaluates to "yes":
>>> "y" == "y" or "yes"
True
>>> "yes" == "y" or "yes"
'yes'
>>> "no" == "y" or "yes"
'yes'
Both True and 'yes' are considered True for an if-expression.
- deleted 9y ago[deleted]
- elcapitan 9y agoSo the use case where you actually want that exact syntax is "if a == someValue or someVariableIsNotNull"
- dllthomas 9y agoI think you read the parent as saying "I expect that Python currently reports this as a type error", which of course is not the case. I suspect, however, that the parent intended something more along the lines of "Other things being equal, the better language would reject this as a type error."