A curious bit of code...

Ethan Furman ethan at stoneleaf.us
Thu Feb 13 16:33:47 EST 2014


On 02/13/2014 01:01 PM, Chris Angelico wrote:
> On Fri, Feb 14, 2014 at 7:55 AM, Emile van Sebille <emile at fenx.com> wrote:
>> On 2/13/2014 11:59 AM, Zachary Ware wrote:
>>>
>>> In a fit of curiosity, I did some timings:
>>
>>
>> Snip of lots of TMTOWTDT/TIMTOWTDI/whatever... timed examples :)
>>
>> But I didn't see this one:
>>
>> s[::len(s)-1]
>
> .... AAAAAAAAAAAAAAAAAAARGGGGGGGGGGHHHH!
>
> Really, I actually did pause, double-take, and then scream under my
> breath, when I saw that. Yes, it works. But please, if you EVER do
> this, save me the trouble and just submit your code to thedailywtf.com
> straight away!

Oh, it's not that bad!  All you have to do is handle the edge case of an empty string:

s[::len(s)-1 if s else True]

*ducks and runs*

--
~Ethan~



More information about the Python-list mailing list