Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Similar thing happens in Python (assuming my knowledge hasn't gone out of date)

  x = 2
  print(x is 2) // true
  x = 200
  print(x is 200) // false


I think a big difference is Java you have to switch between `==` and `Object.equals` depending whether your type is a primitive (e.g. int) or a reference (e.g. Integer).

In Python the cases where you’d be using `is` are a lot more restricted, and a bit of an optimisation (although most style guides will require it in the cases where it makes sense).

There’s basically 3 cases where you’d use `is` in Python:

- the standard singletons None, True, and False (and for the latter two you’d usually use truthiness anyway)

- placeholder singletons (e.g. for default values when you need something other than None)

- actual identity check between arbitrary objects (which very rarely happens IME)




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

Search: