Proposed new collection methods

Terry Reedy tjreedy at udel.edu
Mon Aug 8 03:32:04 CEST 2005


"Mike Meyer" <mwm at mired.org> wrote in message 
news:86d5oqvfqf.fsf at bhuda.mired.org...
> Another thread pointed out a couple of methods that would be nice to
> have on Python collections: find and inject.

Since Python does not have a collections superclass, I am puzzled as to 
what you are really proposing.

> find with no arguments is identical to the any function,
> except it's [find is] a method.

Again, a method of what?

The virtue of functions of iterables is that once written, the 
functionality immediately becomes available to *EVERY* iterable -- for 
free.  Moreover, anybody can write one of the infinite number of such 
functions and use it *without* begging Guido (or Mats) to please add it to 
a perhaps already overly long list of methods of the collection superclass.

> An alternative to adding a new method to collections would be adding a
> new keyword argument - test - to the list index method. This would
> remove the overlap of functionality with any. However, it would also
> mean you couldn't use it on anything but lists (and presumably
> strings). I'm not sure how serious a restriction that is.

Compared to the infinite universe of possible iterables, the restriction is 
pretty severe.  Index should perhaps be turned into a universal iterable 
function.  You could suggest that any() get an optional keyword or 2nd 
param.

In Python, generic functions are functions.  Methods are functions properly 
restricted to one or a few classes and perhaps subclasses.

>      def inject(self, op, init = None):
>          return reduce(op, self, init)

You aren't seriously suggesting that reduce be renamed to something more 
obscure, even contradictory, are you?  Inject means to add something into, 
while reduce usually means to pull a summary out of.  Perhaps I missed 
something.

Terry J. Reedy






More information about the Python-list mailing list