Closures in leu of pointers?

Joshua Landau joshua.landau.ws at gmail.com
Sat Jun 29 16:02:41 EDT 2013


On 29 June 2013 20:42, Tim Chase <tim at thechases.com> wrote:
> On 2013-06-29 19:19, Steven D'Aprano wrote:
>> Nobody ever asks why Python doesn't let you sort an int, or take
>> the square of a list...
>
> just to be ornery, you can sort an int:
>
>>>> i = 314159265
>>>> ''.join(sorted(str(i)))
> '112345569'

To be yet more ornery, you are merely sorting the string
representation of an int.

You can sort an int, though:

    [1].sort()

You may say "No! You are sorting a *list*!" However, is it not fair to
say that with:

    [1, 2, 3, 4, 5].sort()

you are sorting integers? That is just a common english idiom. Hence,
"[1].sort()" sorts an int.

> And I suppose, depending on how you define it, you can square a list:

>From Wikipedia, "a square is the result of multiplying a number, or
other expression, by itself. In other words, squaring is
exponentiation to the power 2."

This means that the only logical definitions would be "list*list" and "list**2".

However, it can be done!

    class PowList(list):
        def __pow__(self, other): pass

    PowList([1, 2, 3])**2

// Because being a pedant is more fun when you're doing it competitively //



More information about the Python-list mailing list