String Identity Test

Gary Herron gherron at islandtraining.com
Wed Mar 4 08:56:58 CET 2009


Avetis KAZARIAN wrote:
> 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?
>   

However the implementors want. 

That may seem a flippant answer, but it's actually accurate.  The choice 
of whether a new string reuses an existing string or creates a new one 
is *not* a Python question, but rather a question of implementation.  
It's a matter of efficiency, and as such each implementation/version of 
Python may make its own choices.   Writing a program that depends on the 
string identity policy would be considered an erroneous program, and 
should be avoided. 

The question now is:  Why do you care?   The properties of strings do 
not depend on the implementation's choice, so you shouldn't care because 
of programming considerations.  Perhaps it's just a matter of curiosity 
on your part.


Gary Herron



>
> --
> Avétis KAZARIAN
> --
> http://mail.python.org/mailman/listinfo/python-list
>   




More information about the Python-list mailing list