| 710 | def __cmp__(self, other): |
| 711 | """ |
| 712 | EXAMPLES:: |
| 713 | |
| 714 | sage: c = CombinatorialObject([1,2,3]) |
| 715 | sage: d = CombinatorialObject([3,2,1]) |
| 716 | sage: cmp(c, d) |
| 717 | -1 |
| 718 | sage: cmp(d, c) |
| 719 | 1 |
| 720 | sage: cmp(c, c) |
| 721 | 0 |
| 722 | |
| 723 | Check that :trac:`14065` is fixed:: |
| 724 | |
| 725 | sage: from sage.structure.element import Element |
| 726 | sage: class Foo(CombinatorialObject, Element): pass |
| 727 | sage: L = [Foo([4-i]) for i in range(4)]; L |
| 728 | [[4], [3], [2], [1]] |
| 729 | sage: sorted(L, cmp) |
| 730 | [[1], [2], [3], [4]] |
| 731 | sage: f = Foo([4]) |
| 732 | sage: f == None |
| 733 | False |
| 734 | sage: f != None |
| 735 | True |
| 736 | |
| 737 | .. WARNING:: |
| 738 | |
| 739 | :class:`CombinatorialObject` must come **before** :class:`Element` |
| 740 | for this to work becuase :class:`Element` is ahead of |
| 741 | :class:`CombinatorialObject` in the MRO (method resolution |
| 742 | order):: |
| 743 | |
| 744 | sage: from sage.structure.element import Element |
| 745 | sage: class Bar(Element, CombinatorialObject): |
| 746 | ... def __init__(self, l): |
| 747 | ... CombinatorialObject.__init__(self, l) |
| 748 | ... |
| 749 | sage: L = [Bar([4-i]) for i in range(4)] |
| 750 | sage: sorted(L, cmp) |
| 751 | Traceback (most recent call last): |
| 752 | ... |
| 753 | NotImplementedError: BUG: sort algorithm for elements of 'None' not implemented |
| 754 | """ |
| 755 | if isinstance(other, CombinatorialObject): |
| 756 | if self._list == other._list: |
| 757 | return 0 |
| 758 | if self._list < other._list: |
| 759 | return -1 |
| 760 | return 1 |
| 761 | if self._list == other: |
| 762 | return 0 |
| 763 | if self._list < other: |
| 764 | return -1 |
| 765 | return 1 |
| 766 | |
| 917 | def __nonzero__(self): |
| 918 | """ |
| 919 | Return ``True`` if ``self`` is non-zero. |
| 920 | |
| 921 | We consider a list to be zero if it has length zero. |
| 922 | |
| 923 | TESTS:: |
| 924 | |
| 925 | sage: c = CombinatorialObject([1,2,3]) |
| 926 | sage: not c |
| 927 | False |
| 928 | sage: c = CombinatorialObject([]) |
| 929 | sage: not c |
| 930 | True |
| 931 | |
| 932 | Check that :trac:`14065` is fixed:: |
| 933 | |
| 934 | sage: from sage.structure.element import Element |
| 935 | sage: class Foo(CombinatorialObject, Element): pass |
| 936 | ... |
| 937 | sage: f = Foo([4]) |
| 938 | sage: not f |
| 939 | False |
| 940 | sage: f = Foo([]) |
| 941 | sage: not f |
| 942 | True |
| 943 | |
| 944 | .. WARNING:: |
| 945 | |
| 946 | :class:`CombinatorialObject` must come **before** :class:`Element` |
| 947 | for this to work becuase :class:`Element` is ahead of |
| 948 | :class:`CombinatorialObject` in the MRO (method resolution |
| 949 | order):: |
| 950 | |
| 951 | sage: from sage.structure.element import Element |
| 952 | sage: class Bar(Element, CombinatorialObject): |
| 953 | ... def __init__(self, l): |
| 954 | ... CombinatorialObject.__init__(self, l) |
| 955 | ... |
| 956 | sage: b = Bar([4]) |
| 957 | sage: not b |
| 958 | Traceback (most recent call last): |
| 959 | ... |
| 960 | AttributeError: 'NoneType' object has no attribute 'zero_element' |
| 961 | """ |
| 962 | return self._list.__len__() > 0 |
| 963 | |