Akira Li added the comment:

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'}

----------
nosy: +akira

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue22089>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to