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

It's puzzling that "x is 17" should work but then "1000 + 1 is 1001" doesn't.

The reason is that Python pre-allocates 262 numbers at initialization (-5 to 256) and the allocation uses a different method/structure for bigger numbers.

  >>> x = 256
  >>> x is 256
  True
  >>> id(x)
  94900419138400
  >>> id(256)
  94900419138400

  >>> x = 257
  >>> x is 257
  False
  >>> id(x)
  94900419504344
  >>> id(257)
  94900419504368
Reference: https://www.laurentluce.com/posts/python-integer-objects-imp...



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

Search: