Comment

Comment by o11c

Non-CPython implementations may pass by value as an optimization for certain immutable builtin types. This is visible to code using `is`.

(It's surprisingly difficult to implement a rigorous way to detect this vs compile-time constant evaluation though; note that identical objects of certain types are pooled already when generating/loading bytecode files. I don't think any current implementation is smart enough to optimize the following though)

  $ python3 -c 'o = object(); print(id(o) is id(o))'
  False
  $ pypy3 -c 'o = object(); print(id(o) is id(o))'
  True
  $ jython -c 'o = object(); print(id(o) is id(o))'
  True
parent o11c 2 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton