[Python-Dev] collections module

Alex Martelli aleaxit at yahoo.com
Fri Jan 9 10:56:40 EST 2004


On Friday 09 January 2004 04:20 pm, Skip Montanaro wrote:
>     Raymond> I would like to establish a new module for some collection
>     Raymond> classes.
>
>     Raymond> The first type would be a bag, modeled after one of
> Smalltalk's Raymond> key collection classes (similar to MultiSets in C++
> and bags in Raymond> Objective C).
>
> I'm neither a Smalltalk, C++ or Objective C programmer.  How is a bag
> different from a set?

In a bag, or multiset, a certain item can be in the collection a given 
_number_ of times, rather than just "either being there or not" as in a set.

> In my GE days (pre-Python) we had a collection class
> in a proprietary language which was used as a proxy.  You'd send a message
> to the collection and it would forward it to all the elements it contained.
> This was rather convenient.  You could tell all the actors to turn and face
> the camera, for example.
>
> Oddly enough, although we used collections heavily, I never really missed
> that functionality in Python.  I implemented it once or twice, but never
> used it much.  Different style of programming I guess.

Yes, there are "proxying-containers" like that in the Python Cookbook, too --
and I've noticed I also don't use them.  "for item in collection: item.turn()" 
is just too nice and general an idiom (with the per-item processing being 
expressible inline, which is often a big clarity win in Python) to easily 
yield to replacement by collection.each(lambda x: x.turn()) or the like...


Alex




More information about the Python-Dev mailing list