String Identity Test
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Wed Mar 4 09:30:44 EST 2009
En Wed, 04 Mar 2009 07:07:44 -0200, Avetis KAZARIAN <avetis.k at gmail.com>
escribió:
> 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 "===".
PHP '==' has no direct correspondence in Python. '===' in PHP is more like
'==' in Python (but not exactly the same).
In PHP, $x === $y is true if both variables are of the same type *and*
both have the same value. $x == $y checks only the values, doing type
conversions as needed, even string -> number; there is no equivalent
operator in Python. PHP === is called "identity" but isn't related to the
"is" operator in Python; there is no identity test in PHP with the Python
semantics.
PHP:
1 == 1
TRUE
1 == 1.0
TRUE
1 == "1"
TRUE
1 == "1.0"
TRUE
1 === 1
TRUE
1 === 1.0
FALSE
1 === "1"
FALSE
1 === "1.0"
FALSE
array(1,2,3) == array(1,2,3)
TRUE
array(1,2,3) === array(1,2,3)
TRUE
Python:
1 == 1
True
1 == 1.0
True
1 == "1"
False
1 == "1.0"
False
[1,2,3] == [1,2,3]
True
[1,2,3] is [1,2,3]
False
So, don't try to translate concepts from one language to another. (Ok,
it's natural to try to do that if you know PHP, but doesn't work. You have
to know the differences).
--
Gabriel Genellina
More information about the Python-list
mailing list