[Python-3000-checkins] r60683 - in python/branches/py3k: Doc/library/collections.rst Lib/_abcoll.py
raymond.hettinger
python-3000-checkins at python.org
Sat Feb 9 04:25:08 CET 2008
Author: raymond.hettinger
Date: Sat Feb 9 04:25:08 2008
New Revision: 60683
Modified:
python/branches/py3k/Doc/library/collections.rst
python/branches/py3k/Lib/_abcoll.py
Log:
Document how to use Set and MutableSet as a mixin.
Modified: python/branches/py3k/Doc/library/collections.rst
==============================================================================
--- python/branches/py3k/Doc/library/collections.rst (original)
+++ python/branches/py3k/Doc/library/collections.rst Sat Feb 9 04:25:08 2008
@@ -58,7 +58,7 @@
``insert``, ``remove``, and ``__iadd__``
and ``__len__``
-:class:`Set` :class:`Sized`, ``__len__``, ``__le__``, ``__lt__``, ``__eq__``, ``__ne__``,
+:class:`Set` \(1) \(2) :class:`Sized`, ``__len__``, ``__le__``, ``__lt__``, ``__eq__``, ``__ne__``,
:class:`Iterable`, ``__iter__``, and ``__gt__``, ``__ge__``, ``__and__``, ``__or__``
:class:`Container` ``__contains__`` ``__sub__``, ``__xor__``, and ``isdisjoint``
@@ -100,6 +100,23 @@
s2 = ListBasedSet('defghi')
overlap = s1 & s2 # The __and__() method is supported automatically
+Notes on using :class:`Set` and :class:`MutableSet` as a mixin:
+
+(1)
+ Since some set operations create new sets, the default mixin methods need
+ a way to create new instances from an iterable. The class constructor is
+ assumed to have a signature in the form ``ClassName(iterable)``.
+ That assumption is factored-out to a singleinternal classmethod called
+ :meth:`_from_iterable` which calls ``cls(iterable)`` to produce a new set.
+ If the :class:`Set` mixin is being used in a class with a different
+ constructor signature, you will need to override :meth:`from_iterable`
+ with a classmethod that can construct new instances from
+ an iterable argument.
+
+(2)
+ To override the comparisons (presumably for speed, as the
+ semantics are fixed), redefine :meth:`__le__` and
+ then the other operations will automatically follow suit.
(For more about ABCs, see the :mod:`abc` module and :pep:`3119`.)
Modified: python/branches/py3k/Lib/_abcoll.py
==============================================================================
--- python/branches/py3k/Lib/_abcoll.py (original)
+++ python/branches/py3k/Lib/_abcoll.py Sat Feb 9 04:25:08 2008
@@ -207,9 +207,9 @@
'''Construct an instance of the class from any iterable input.
Must override this method if the class constructor signature
- will not accept a frozenset for an input.
+ does not accept an iterable for an input.
'''
- return cls(frozenset(it))
+ return cls(it)
def __and__(self, other):
if not isinstance(other, Iterable):
More information about the Python-3000-checkins
mailing list