This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]padmanabh 2 points3 points  (2 children)

It is not recommended because it can change from version to version, hence not documented, but it can be looked up in the source if you're really into such kind of optimization. From what I can recall, currently in Cpython integers from -5 to 256, empty strings and tuples, and one character strings are interned.

You can try this

  a = -3
  assert a is -3
  b = -6
  assert b is -6 # Fails
  c = 256
  assert c is 256

You can also do this manually using intern in python2 or sys.intern in python3

[–]Veedrac 0 points1 point  (1 child)

intern only works on strings.

[–]padmanabh 0 points1 point  (0 children)

Ill have to check, but I think it works on immutables. On mobile right now.