Python functools total_ordering














































Python functools total_ordering



Total_ordering 
It is a class decorator that fills in the missing comparison methods (__lt__, __gt__, __eq__, __le__, __ge__). If a class is given which defines one or more comparison methods, @total_ordering automatically supplies the rest as per the given definitions. However, the class must define one of __lt__(), __le__(), __gt__(), or __ge__() and 
additionally, the class should supply an __eq__() method.

Example:

from functools import total_ordering
  
@total_ordering
class N:
    def __init__(self, value):
        self.value = value
    def __eq__(self, other):
        return self.value == other.value
  
    # Reverse the function of 
    # '<' operator and accordingly
    # other rich comparison operators
    # due to total_ordering decorator
    def __lt__(self, other):
        return self.value > other.value
  
  
print('6 > 2 :', N(6)>N(2))
print('3 < 1 :', N(3)<N(1))
print('2 <= 7 :', N(2)<= N(7))
print('9 >= 10 :', N(9)>= N(10))
print('5 == 5 :', N(5)== N(5))

Output :

6 > 2 : False
3 < 1 : True
2 = 10 : True
5 == 5 : True


More Articles of Anshuman Gupta:

Name Views Likes
Python calendar leapdays 637 11
Python functools cmp_to_key 1142 12
Python collections ordereddict 733 12
Python tkinter : Button 940 12
Python calendar formatmonth 568 11
Python tkinter : Canvas 996 11
Python tkinter Radiobutton 787 13
Python program to get the value of Pi to n number of decimal places. 1174 12
Python tkinter : Introduction 55 2
Python calendar firstweekday 623 12
Python calendar Introduction 690 12
Python tkinter Scrollbar 870 13
Write a Python program to create a label and change the label font style (font name, bold, size) using tkinter module 1348 13
Python tkinter Menubutton 768 13
The most efficient way of generating prime numbers 717 12
Python functools total_ordering 655 14
Python tkinter : Entry 870 12
Python calendar isleap 631 12
Python project to create a pages automatically like WordPress in Django from admin panel 1642 12
How to download and install Python Latest Version on Android 827 12
Python calendar formatyear 611 12
Python re Metacharacters 664 12
Python tkinter Listbox 871 14
Python re search 674 12
Python tkinter Frame 731 11
Python functools update_wrapper 709 12
Python collections deque 619 12
Python calendar setfirstweekday 534 13
Python tkinter Label 751 12
Python re subn 617 12
Python re Introduction 738 13
Python collections Introduction 673 12
Python calendar pryear 548 13
Python calendar prmonth 559 11
Best approach to find the factorial of a number 496 12
Python tkinter : Checkbutton 770 12
Python functools Wraps 565 12
Python re split 704 12
Python calendar iterweekdays 601 12
Python re findall 889 12
Python re sub 642 12
Python functools Introduction 585 12
Python tkinter : Introduction 1221 12
python collections counter 719 12
Python functools Lru_cache 884 12
Python tkinter Scale 802 13
Python calendar Introduction 60 10
Python tkinter Spinbox 770 14
Python tkinter Message 697 13
Python tkinter Project 918 13
Python collections chainmap 539 12
Python functools Reduce 590 12
Python calendar itermonthdates 911 12
Python re match 682 12

Comments