Spyros Kappalamda
Spyros Kappalamda

Reputation: 55

Python multiple strings point to same heap memory object if value have less than 4 DIFFERENT characters

in addition to the answers provided here

For example :

q = "asdasdasdsadsadsadsadsadsadsadsadsad"
a = "asdasdasdsadsadsadsadsadsadsadsadsad"

>>> a is q
True

even though the strings are long(way longer than 4 characters), python still points to the same object in memory no matter how many times i concatenate "asd", but...

..if i add,lets say "e", that makes the unique characters 4 (a,s,d and e) and python now creates a value object in heap.

q = "asdasdasdsadsadsadsadsadsadsadsadsade"
a = "asdasdasdsadsadsadsadsadsadsadsadsade"

>>> a is q
False

Interesting.

Can someone elaborate on this?

Edit:

Now it is True, but if I add a special character in both variables then it returns False

Upvotes: 2

Views: 49

Answers (0)

Related Questions