Message224219
Set has no __ior__ method but MutableSet has:
class MySet(MutableSet):
update = MutableSet.__ior__
Unlike set.__ior__; MutableSet.__ior__ accepts an arbitrary iterable
and therefore MutableSet.update is redundant.
set.__ior__ doesn't accept an arbitrary iterable:
>>> s = set()
>>> s.update('ab')
>>> s |= 'ab'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |=: 'set' and 'str'
>>> s |= set('ab')
>>> s
{'a', 'b'}
MutableSet.__ior__ does accept an arbitrary iterable:
from collections.abc import MutableSet
class UpperSet(MutableSet):
"""Like set() but stores items in upper case."""
def __init__(self, iterable=()):
self._set = set()
self |= iterable
def _key(self, item):
return item.upper()
update = MutableSet.__ior__
# implement MutableSet abstract methods
def __contains__(self, item):
return self._key(item) in self._set
def __iter__(self):
return iter(self._set)
def __len__(self):
return len(self._set)
def add(self, item):
self._set.add(self._key(item))
def discard(self, item):
self._set.discard(self._key(item))
Example:
s = UpperSet('σs')
assert 'σ' in s and 'Σ' in s and 'S' in s and 'ς' in s and 'ſ' in s
s.update('dzẞ') # or s |= 'dzẞ'
assert 'Dz' in s and 'DZ' in s and 'ß' not in s and 'SS' not in s
s |= 'ß' # or s.update('ß')
assert s == {'Σ', 'S', 'DZ', 'ẞ', 'SS'} |
|
Date |
User |
Action |
Args |
2014-07-29 10:54:36 | akira | set | recipients:
+ akira, rhettinger, josh.r, roy.wellington |
2014-07-29 10:54:36 | akira | set | messageid: <1406631276.29.0.331609431288.issue22089@psf.upfronthosting.co.za> |
2014-07-29 10:54:36 | akira | link | issue22089 messages |
2014-07-29 10:54:35 | akira | create | |
|