What is wrong in my list comprehension?

MRAB google at mrabarnett.plus.com
Mon Feb 2 18:19:38 CET 2009


J Kenneth King wrote:
> Chris Rebert <clp2 at rebertia.com> writes:
> 
>> Python 2.6 (r26:66714, Nov 18 2008, 21:48:52)
>> [GCC 4.0.1 (Apple Inc. build 5484)] on darwin
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> bool(-1)
>> True
>>
>> str.find() returns -1 on failure (i.e. if the substring is not in the
>> given string).
>> -1 is considered boolean true by Python.
> 
> That's an odd little quirk... never noticed that before.
> 
> I just use regular expressions myself.
> 
> Wouldn't this be something worth cleaning up? It's a little confusing
> for failure to evaluate to boolean true even if the relationship isn't
> direct.
> 
str.find() returns the index (position) where the substring was found. 
Because string indexes start at 0 the returned value is -1 if it's not 
found.

In those languages where string indexes start at 1 the returned value is 
0 if not found.




More information about the Python-list mailing list