Commit Graph

13 Commits

Author SHA1 Message Date
Guido van Rossum dc61cdf6c0 Change the binary operators |, &, ^, - to return NotImplemented rather
than raising TypeError when the other argument is not a BaseSet.  This
made it necessary to separate the implementation of e.g. __or__ from
the union method; the latter should not return NotImplemented but
raise TypeError.  This is accomplished by making union(self, other)
return self|other, etc.; Python's binary operator machinery will raise
TypeError.

The idea behind this change is to allow other set implementations with
an incompatible internal structure; these can provide union (etc.) with
standard sets by implementing __ror__ etc.

I wish I could do this for comparisons too, but the default comparison
implementation allows comparing anything to anything else (returning
false); we don't want that (at least the test suite makes sure
e.g. Set()==42 raises TypeError).  That's probably fine; otherwise
other set implementations would be constrained to implementing a hash
that's compatible with ours.
2002-08-22 17:23:33 +00:00
Raymond Hettinger d9c9151a53 Now that __init__ transforms set elements, we know that all of the
elements are hashable, so we can use dict.update() or dict.copy()
for a C speed Set.copy().
2002-08-21 13:20:51 +00:00
Raymond Hettinger 80d21af614 Sped ._update() method by factoring try/except out of the inner loop. 2002-08-21 04:12:03 +00:00
Guido van Rossum 9f87293bf5 Ouch. The test suite *really* needs work!!!!! There were several
superficial errors and one deep one that aren't currently caught.  I'm
headed for bed after this checkin.

- Fixed several typos introduced by Raymond Hettinger (through
  cut-n-paste from my template): it's _as_temporarily_immutable, not
  _as_temporary_immutable, and moreover when the element is added, we
  should use _as_immutable.

- Made the seq argument to ImmutableSet.__init__ optional, so we can
  write ImmutableSet() to create an immutable empty set.

- Rename the seq argument to Set and ImmutableSet to iterable.

- Add a Set.__hash__ method that raises a TypeError.  We inherit a
  default __hash__ implementation from object, and we don't want that.
  We can then catch this in update(), so that
  e.g. s.update([Set([1])]) will transform the Set([1]) to
  ImmutableSet([1]).

- Added the dance to catch TypeError and try _as_immutable in the
  constructors too (by calling _update()).  This is needed so that
  Set([Set([1])]) is correctly interpreted as
  Set([ImmutableSet([1])]).  (I was puzzled by a side effect of this
  and the inherited __hash__ when comparing two sets of sets while
  testing different powerset implementations: the Set element passed
  to a Set constructor wasn't transformed to an ImmutableSet, and then
  the dictionary didn't believe the Set found in one dict it was the
  same as ImmutableSet in the other, because the hashes were
  different.)

- Refactored Set.update() and both __init__() methods; moved the body
  of update() into BaseSet as _update(), and call this from __init__()
  and update().

- Changed the NotImplementedError in BaseSet.__init__ to TypeError,
  both for consistency with basestring() and because we have to use
  TypeError when denying Set.__hash__.  Together those provide
  sufficient evidence that an unimplemented method needs to raise
  TypeError.
2002-08-21 03:20:44 +00:00
Guido van Rossum 26588222b3 Add Raymond H to the list of authors; add some XXX comments about
possible API improvements.
2002-08-21 02:44:04 +00:00
Raymond Hettinger 43db0d6a2c Fast size check for sub/super set tests 2002-08-21 02:22:08 +00:00
Raymond Hettinger de6d697987 Optimize try/except ordering in sets.py.
Gains a 5:1 speed-up for membership testing by
handling the most common case first (the case
where the element is hashable).

Closes SF Patch 597444.
2002-08-21 01:35:29 +00:00
Raymond Hettinger ede3a0da8b Minor typo 2002-08-20 23:34:01 +00:00
Guido van Rossum c9196bc88d Rename popitem() to pop(). (An idea from SF patch 597444.) 2002-08-20 21:51:59 +00:00
Guido van Rossum 5033b36c44 Move __init__ from BaseSet into Set and ImmutableSet. This causes a
tiny amount of code duplication, but makes it possible to give BaseSet
an __init__ that raises an exception.
2002-08-20 21:38:37 +00:00
Guido van Rossum 290f1870f1 Add a note reminding the reader that sets are not sequences. I
received feedback that was based in the misunderstanding that sets
were sequences.
2002-08-20 20:05:23 +00:00
Guido van Rossum 0b650d7565 Fix typo in __slots__ of ImmutableSet. 2002-08-19 16:29:58 +00:00
Guido van Rossum d6cf3af8f7 Set classes and their unit tests, from sandbox. 2002-08-19 16:19:15 +00:00