String Identity Test

Avetis KAZARIAN avetis.k at gmail.com
Wed Mar 4 08:22:07 CET 2009


After reading the discussion about the same subject ( From: "Thomas
Moore" <jsfrank.c... at msa.hinet.net> Date: Tue, 1 Nov 2005 21:45:56
+0800 ), I tried myself some tests with some confusing results (I'm a
beginner with Python, I'm coming from PHP)



# 1. Short alpha-numeric String without space

a = "b747"
b = "b747"

>>> a is b
True



# 2. Long alpha-numeric String without space

a =
"averylongstringbutreallyaveryveryverylongstringwithabout68characters"
b =
"averylongstringbutreallyaveryveryverylongstringwithabout68characters"

>>> a is b
True



# 3. Short alpha-numeric String with space

a = "x y"
b = "x y"

>>> a is b
False



# 4. Long alpha-numeric String with space

a = "I love Python it s so much better than PHP but sometimes
confusing"
b = "I love Python it s so much better than PHP but sometimes
confusing"

>>> a is b
False



# 5. Empty String

a = ""
b = ""

>>> a is b
True


# 6. Whitecharacter  String : space

a = " "
b = " "

>>> a is b
False



# 7. Whitecharacter String : new line

a = "\n"
b = "\n"

>>> a is b
False



# 8. Non-ASCII without space

a = "é"
b = "é"

>>> a is b
False



# 9. Non-ASCII with space

a = "é à"
b = "é à"

>>> a is b
False



It seems that any strict ASCII alpha-numeric string is instantiated as
an unique object, like a "singleton" ( a = "x" and b = "x" => a is b )
and that any non strict ASCII alpha-numeric string is instantiated as
a new object every time with a new id.

Conclusion :

How does Python manage strings as objects?


--
Avétis KAZARIAN



More information about the Python-list mailing list