Comparison problem

Tom Anderson twic at urchin.earth.li
Sun Nov 27 01:32:42 CET 2005


On Sat, 26 Nov 2005, Peter Hansen wrote:

> Tom Anderson wrote:
>> On Sat, 26 Nov 2005, Chris wrote:
>> 
>>>   if item[0:1]=="-":
>> 
>> item[0:1] seems a rather baroque way of writing item[0]! I'd actually 
>> suggest writing this line like this:
>
> Actually, it's not so much baroque as it is safe... item[0] will fail if 
> the string is empty, while item[0:1] will return '' in that case.

Ah i didn't realise that. Whether that's safe rather depends on what the 
subsequent code does with an empty string - an empty string might be some 
sort of error (in this particular case, it would mean that the loop test 
had gone wrong, since bool("") == False), and the slicing behaviour would 
constitute silent passing of an error.

But, more importantly, egad! What's the thinking behind having slicing 
behave like that? Anyone got any ideas? What's the use case, as seems to 
be the fashionable way of putting it these days? :)

tom

-- 
This should be on ox.boring, shouldn't it?



More information about the Python-list mailing list