Python Tutorial Was: Guido's regrets: filter and map

Jeremy Fincher tweedgeezer at hotmail.com
Wed Nov 27 04:43:17 EST 2002


Erik Max Francis <max at alcyone.com> wrote in message news:<3DE40E21.A547F49E at alcyone.com>...
> Jeremy Fincher wrote:
> 
> > The biggest problem with filter, map, and reduce is that they're
> > functions, not methods.
> 
> But since they can operate on _any_ sequence, this makes the most sense.

Since they operate on any sequence, they should be methods of any
sequence.  As functions, yes, they operate on all sequences, but they
only return lists.  As methods, map/filter would return sequences of
the same type they operated on, which is (IMO) far more appropriate.

In short, why should a map on a string return a list instead of a
string?  Why should a filter on a binary tree return a list instead of
another binary tree?

> But there are several other things that can act as sequence objects,
> such as strings, tuples, or user-defined types that don't derive from
> list but define a __getitem__ method.  filter, map, and reduce really do
> belong in the realm of standalone functions, not methods, because they
> can apply to any type that _acts_ as a list, whether or not it is a
> list, a builtin sequence, or even something which at first sight doesn't
> appear to be a sequence type at all (but acts the part).

But I think it would be more appropriate for map/filter to return
sequences of the same type as their argument sequences, not just
return lists blindly.  And that requires that map/filter be methods,
not functions over sequences.

Jeremy



More information about the Python-list mailing list