[Tutor] trouble with function-- trying to check differences btwn 2 strings

Bob Gailer bgailer at alum.rpi.edu
Wed Mar 7 02:16:37 CET 2007


David Perlman wrote:
> On Mar 6, 2007, at 4:28 PM, wesley chun wrote:
>
>   
>>>  >>> x=('i' in 'i')
>>>  >>> x
>>> True
>>>  >>> y='i'
>>>  >>> x==y
>>> False
>>>       
>> you're right when you talk about "casting" altho that's not what
>> python does.  it merely performs an object value comparison when you
>> use '=='.  for example, change your code above to:
>>
>>     
>>>>> True == 'i'    # because this is what you're really doing with x==y
>>>>>           
>> False
>>
>> so the reason why you get a false is that those 2 values *are*
>> different from each other, even if their boolean truthfulness may be
>> the same:
>>
>>     
>>>>> bool(True) == bool('i')
>>>>>           
>> True
>>
>> how's *that* for casting?  :-)
>>
>> just remember that the interpreter compares *values* and not boolean
>> truthfulness, and you'll be ok.  if you really want the latter, then
>> use bool().
>>
>> hope this helps!
>> -- wesley
>>     
>
> This helps convince me that I still don't understand why the original  
> code snippet worked at all.  :)
>
> These code examples make perfect sense.  This one doesn't, and  
> appears to be an inconsistency:
>
>  >>> word2 = 'hello'
>  >>> item = 'e'
>  >>> item in word2
> True
>  >>> item == item in word2
> True
>   
Take a look in 5.9 Comparisons in the Language Reference:

"Comparisons can be chained arbitrarily, e.g., |x < y <= z| is 
equivalent to |x < y and y <= z|, except that |y| is evaluated only once 
(but in both cases |z| is not evaluated at all when |x < y| is found to 
be false)."

Applying this to

item == item in word2

yields:

(item == item) and (item in word2)

 
Take it from there...

-- 
Bob Gailer
510-978-4454



More information about the Tutor mailing list