Tuesday, November 04, 2014

Partial fun

My daughter suggested to me that

To sort in descending order in python, you should be able to do
detros() instead of sorted( ... reverse = True)

This is what I suggested to her:

>>> from functools import partial
>>> detros = partial(sorted, reverse=True)
>>> detros([1, 3, 4, 2])
[4, 3, 2, 1]

I love Python. :-)

1 comment:

  1. def detros(iterable):
    return sorted(iterable, reverse=True)


    But yeah, that ignores the other 2 possible arguments :)

    ReplyDelete

Spammers: none shall pass.

Note: Only a member of this blog may post a comment.