A curious bit of code...
ethan at stoneleaf.us
Thu Feb 13 20:23:59 CET 2014
On 02/13/2014 11:17 AM, Neil Cerutti wrote:
> On 2014-02-13, forman.simon at gmail.com <forman.simon at gmail.com>
>> I ran across this and I thought there must be a better way of
>> doing it, but then after further consideration I wasn't so
>> if key[:1] + key[-1:] == '<>': ...
>> Some possibilities that occurred to me:
>> if key.startswith('<') and key.endswith('>'): ...
>> if (key[:1], key[-1:]) == ('<', '>'): ...
>> I haven't run these through a profiler yet, but it seems like
>> the original might be the fastest after all?
> I think the following would occur to someone first:
> if key == '<' and key[-1] == '>':
> It is wrong to avoid the obvious. Needlessly ornate or clever
> code will only irritate the person who has to read it later; most
> likely yourself.
Not whet the obvious is wrong:
-> key = ''
--> if key == '<' and key[-1] == '>':
... print "good key!"
... print "bad key"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
More information about the Python-list