A curious bit of code...

Roy Smith roy at panix.com
Thu Feb 13 19:45:44 CET 2014


In article <4cc09129-43ee-4205-a24c-03f92b594abc at googlegroups.com>,
 forman.simon at gmail.com wrote:

> 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 sure.
> 
>   if key[:1] + key[-1:] == '<>': ...
> 
> 
> Some possibilities that occurred to me:
> 
>   if key.startswith('<') and key.endswith('>'): ...
> 
> and:
> 
>   if (key[:1], key[-1:]) == ('<', '>'): ...

if re.match(r'^<.*>$', key):

sheesh.

(if you care how fast it is, pre-compile the pattern)



More information about the Python-list mailing list