[Python-Dev] decorate-sort-undecorate

Ian Bicking ianb at colorstudy.com
Wed Oct 15 21:22:14 EDT 2003

On Wednesday, October 15, 2003, at 07:27 PM, Peter Norvig wrote:
> As it turns out, I have a proposed syntax for something I call an
> "accumulation display", and with it I was able to implement and test a
> SortBy in about a minute.  It uses the syntax
>>>> [SortBy: abs(x) for x in (-2, -4, 3, 1)]
> [1, -2, 3, -4]
> where SortBy is an expression (in this case an identifier bound to a
> class object), not a keyword.  Other examples of accumulation displays
> include:
>     [Sum: x*x for x in numbers]
>     [Product: Prob_spam(word) for word in email_msg]
>     [Min: temp(hour) for hour in range(24)]
>     [Top(10): humor(joke) for joke in jokes]
>     [Argmax: votes[c] for c in candidates]
> You can read the whole proposal at http:///www.norvig.com/pyacc.html

Neat.  +1.

I think it would be nice if accumulators were created more like 
iterators, maybe with an __accum__ method.  Then builtins like min and 
max could be turned into accumulators, kind of like the int function 
was turned into a class.  Then you also wouldn't have to check for and 
instantiate classes, which seems a little crude.

Then if a sorted() function/class was added to builtins, and it was 
also an accumulator, you'd be all set.  And all the sort method haters 
out there (they number many!) would be happy.

But [sorted: abs(x) for x in lst] doesn't seem right at all, it should 
return a list of abs(x) sorted by x, not a list of x sorted by abs(x).  
[sorted.by: abs(x) for x in lst] is perhaps more clever than practical 
-- it could work and it reads nicely, but it doesn't look normal.

Ian Bicking | ianb at colorstudy.com | http://blog.ianbicking.org

More information about the Python-Dev mailing list