A curious bit of code...

Mark Lawrence breamoreboy at yahoo.co.uk
Thu Feb 13 20:09:56 CET 2014


On 13/02/2014 18:37, 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:]) == ('<', '>'): ...
>
>
> I haven't run these through a profiler yet, but it seems like the original might be the fastest after all?
>

All I can say is that if you're worried about the speed of a single line 
of code like the above then you've got problems.  Having said that, I 
suspect that using an index to extract a single character has to be 
faster than using a slice, but I haven't run these through a profiler yet :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com





More information about the Python-list mailing list