[Python-ideas] Proposal for extending the collections module - bags / multisets, ordered sets / unique lists

Chris Rebert pyideas at rebertia.com
Sat Jul 18 22:32:44 CEST 2009


On Fri, Jul 17, 2009 at 6:54 PM, Michael Lenzen<m.lenzen at gmail.com> wrote:
<snip>
> New ABCs (abstract base classes)
> --------------------------------
<snip>
> Mutable - I think there should be one metaclass for all mutable types just
> like there is currently Iterable, Sized and Container. We could then check
> instanceof(obj, Mutable). MutableSequence, MutableSet and MutableMapping
> would inherit Mutable, or we might be able to just do away with them
> altogether.  There are 3 abstractmethods defined:
>  * pop(self) - This is the simplest to implement, all mutable classes
> already do.
>  * __setitem__(self, key, value) - This is already implemented for list and
> dict but would have to be defined for set, more later
>  * __delitem__(self, key) Same as above.

I think this would be better named MutableCollection personally,
otherwise it's liable to be confused with the more general mutable vs.
immutable types distinction.

<snip>
> Extending set to fit the ABCs
> -----------------------------
> I mentioned before that sets don't currently fit into my model. Here are my
> propositions for the three methods that would need to be implemented.
>  * set.__setitem__(self, elem, value) - Set whether or not elem is in the
> set based on the boolean evaluation of value. s[elem] = 1 would call
> s.add(elem) and s[elem] = False would call s.remove(elem), appropriately
> raising a KeyError if elem not in s. The reason I chose this way is that the
> only thing you can set about an element of a set is whether or not it is
> present.

The implicit bool(), which I assume is what you mean by "boolean
evaluation", here concerns me, as it could mask bugs where something
tries to treat a set like a mapping. I would recommend throwing
TypeError if given a non-int and ValueError if given any integer
besides 0 or 1, so as to more strongly enforce the set axioms.

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-ideas mailing list