String Identity Test

Steve Holden steve at holdenweb.com
Wed Mar 4 09:24:25 EST 2009


Avetis KAZARIAN wrote:
> Gary Herron wrote:
>> 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
> 
> Well, it's not about curiosity, it's more about performance.
> 
> I will make a PHP example (a really quite simple )
> 
> PHP :
> 
> Stat 1 : $aVeryLongString == $anOtherVeryLongString
> Stat 2 : $aVeryLongString === $anOtherVeryLongString
> 
> Stat 2 is really faster than Stat 1 (due to the binary comparison)
> 
> As I said, I'm coming from PHP, so I was wondering if there was such a
> difference in Python.
> 
> Because I was trying to use "is" as for "===".

Suppose you write

a = b

Thereafter, unless some further assignment is made to either a or b, you
are guaranteed that "a is b" returns True.

This is pretty much the only guarantee you have. There is no guarantee
(across all implementations) that

a = some-expression

b = some-equivalent-expression

will leave "a is b" True.

Does PHP really keep only one copy of every string? Sounds like that
could slow string creation down a little. Essentially it's keeping all
strings in a set. Of course you could do that in Python if you wanted,
but it would certainly slow things down.

Anyway, thanks for looking at Python. I hope you continue to enjoy it!

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list