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

Erik Max Francis max at alcyone.com
Wed Nov 27 05:05:54 EST 2002


Jeremy Fincher wrote:

> 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 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.

Yes, it does, but it also means that everyone who ever wants to
implement a sequence type needs to define __filter__ and __map__ methods
(say) or you can't do filtering and mapping with them.  Filter and map
are general algorithms over sequence types -- all you need to use them
is iteration, which is what all sequence types have in common.

The fact that filter and map return lists regardless of the original
sequence type is simply a necessary evil, because it means that it can
now do something meaningful for _all_ sequence types, whether or not
those particular custom types had any special dispensation for filtering
or mapping.  With your approach, if someone makes a sequence-compatible
type but doesn't give me the right methods, I'm out of luck, even though
it's about _algorithms_ not about methods.

Filter and map are about generalized ways of dealing with sequences. 
They end up returning newly-created sequence types, and since the
standard sequence type in Python is a list, they return lists.  If you
want to turn that back into a custom sequence type, you're more than
welcome to do it; in your example, you can just do ''.join(map(..., S))
if you want to apply map to a string S and then return it back into a
string.  If you can only use filter and map when the appropriate method
is defined in the custom type, then you're leaving yourself in a
situation where you can't use filter/map over any sequence even though
there's no logical reason why you shouldn't.  All that a sequence type
needs to define is a __getitem__ method with the appropriate semantics;
you're saying you'd need to define __filter__ and __map__ (say), and if
you take this argument to the fullest, a whole slew of other methods as
well.

Filter and map can _operate_ on any sequence, but they return lists
since that's the logical generic sequence type to return.  If you want
to turn that back into some other custom sequence type after the fact,
just go ahead.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ I'm crying everyone's tears
\__/ Sade
    Bosskey.net: Aliens vs. Predator 2 / http://www.bosskey.net/avp2/
 A personal guide to Aliens vs. Predator 2.



More information about the Python-list mailing list