A set is a finite, iterable container. This class provides concrete generic implementations of all methods except for __contains__, __iter__ and __len__. To override the comparisons (presumably for speed, as the semantics are fixed), redefine __le__ and __ge__, then the other o
| 547 | |
| 548 | |
| 549 | class Set(Collection): |
| 550 | """A set is a finite, iterable container. |
| 551 | |
| 552 | This class provides concrete generic implementations of all |
| 553 | methods except for __contains__, __iter__ and __len__. |
| 554 | |
| 555 | To override the comparisons (presumably for speed, as the |
| 556 | semantics are fixed), redefine __le__ and __ge__, |
| 557 | then the other operations will automatically follow suit. |
| 558 | """ |
| 559 | |
| 560 | __slots__ = () |
| 561 | |
| 562 | def __le__(self, other): |
| 563 | if not isinstance(other, Set): |
| 564 | return NotImplemented |
| 565 | if len(self) > len(other): |
| 566 | return False |
| 567 | for elem in self: |
| 568 | if elem not in other: |
| 569 | return False |
| 570 | return True |
| 571 | |
| 572 | def __lt__(self, other): |
| 573 | if not isinstance(other, Set): |
| 574 | return NotImplemented |
| 575 | return len(self) < len(other) and self.__le__(other) |
| 576 | |
| 577 | def __gt__(self, other): |
| 578 | if not isinstance(other, Set): |
| 579 | return NotImplemented |
| 580 | return len(self) > len(other) and self.__ge__(other) |
| 581 | |
| 582 | def __ge__(self, other): |
| 583 | if not isinstance(other, Set): |
| 584 | return NotImplemented |
| 585 | if len(self) < len(other): |
| 586 | return False |
| 587 | for elem in other: |
| 588 | if elem not in self: |
| 589 | return False |
| 590 | return True |
| 591 | |
| 592 | def __eq__(self, other): |
| 593 | if not isinstance(other, Set): |
| 594 | return NotImplemented |
| 595 | return len(self) == len(other) and self.__le__(other) |
| 596 | |
| 597 | @classmethod |
| 598 | def _from_iterable(cls, it): |
| 599 | '''Construct an instance of the class from any iterable input. |
| 600 | |
| 601 | Must override this method if the class constructor signature |
| 602 | does not accept an iterable for an input. |
| 603 | ''' |
| 604 | return cls(it) |
| 605 | |
| 606 | def __and__(self, other): |