Hacker News new | past | comments | ask | show | jobs | submit login
For-Else: A Weird but Useful Feature in Python (medium.com/techtofreedom)
2 points by yangzhou on April 25, 2021 | hide | past | favorite | 4 comments



I see the logic of this feature, and in a couple of seconds I'll be unable to understand it again.


I keep wanting the else to run when the loop didn't iterate.


I thought about how I'd do this in JS, and it's paradoxically more compact and less confusing without dedicated syntax:

  if (leaders.some(i => i == "Yang")) {
      alert("Yang is a leader!");
  } else {
      alert("Not found Yang!");
  }


It's weird, but still useful at some points.




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

Search: