Hacker News new | past | comments | ask | show | jobs | submit login

I think the sentence in the introduction, "Beginners construct valid Python expressions that don’t do what they seem like they should do.", is meant to include people who aren't coming from any programming language.



Yes, moreover, it should be a type error.


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.


I 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."


So the use case where you actually want that exact syntax is

"if a == someValue or someVariableIsNotNull"




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: