Skip to content

Commit 7f9dd7d

Browse files
authored
[Term Entry] Python functools: total_ordering()
* Term entry for total_ordering class decorator under the functools module. Also, recommending adding Python to Subjects and Class Decorator to tags. * content fixes * Minor changes ---------
1 parent c695b34 commit 7f9dd7d

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
Title: 'total_ordering()'
3+
Description: 'Generates missing comparison methods in a class based on a minimal set of defined ones.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Classes'
9+
- 'Modules'
10+
- 'Python'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/computer-science'
14+
---
15+
16+
In Python, the **`total_ordering()`** decorator from the `functools` module simplifies the creation of fully ordered classes. By defining the `__eq__()` method and one additional comparison method (`__lt__()`, `__le__()`, `__gt__()`, or `__ge__()`), all other rich comparison methods are automatically generated.
17+
18+
This decorator reduces redundant code in custom classes that require complete ordering behavior for comparisons, sorting, and equality checks.
19+
20+
## Syntax
21+
22+
```pseudo
23+
from functools import total_ordering
24+
25+
@total_ordering
26+
class ClassName:
27+
def __eq__(self, other): ...
28+
def __lt__(self, other): ...
29+
```
30+
31+
**Parameters:**
32+
33+
The `total_ordering()` decorator takes no parameters.
34+
35+
**Return value:**
36+
37+
Returns a class with the missing comparison methods (`__le__`, `__gt__`, and `__ge__`) automatically added.
38+
39+
## Example 1: Numeric Wrapper Class
40+
41+
The following example defines a class that compares wrapped numeric values. Only `__eq__()` and `__lt__()` are implemented; the rest are generated automatically:
42+
43+
```py
44+
from functools import total_ordering
45+
46+
@total_ordering
47+
class Number:
48+
def __init__(self, value):
49+
self.value = value
50+
51+
def __eq__(self, other):
52+
return self.value == other.value
53+
54+
def __lt__(self, other):
55+
return self.value < other.value
56+
57+
print(Number(3) < Number(4))
58+
print(Number(5) >= Number(5))
59+
print(Number(7) > Number(1))
60+
```
61+
62+
The outout of this code is:
63+
64+
```shell
65+
True
66+
True
67+
True
68+
```
69+
70+
## Example 2: Ordering Strings by Length
71+
72+
This example demonstrates ordering based on string length instead of direct string comparison:
73+
74+
```py
75+
from functools import total_ordering
76+
77+
@total_ordering
78+
class Word:
79+
def __init__(self, text):
80+
self.text = text
81+
82+
def __eq__(self, other):
83+
return len(self.text) == len(other.text)
84+
85+
def __lt__(self, other):
86+
return len(self.text) < len(other.text)
87+
88+
print(Word("apple") < Word("banana"))
89+
print(Word("kiwi") == Word("pear"))
90+
print(Word("grape") > Word("fig"))
91+
```
92+
93+
The output of this code is:
94+
95+
```shell
96+
True
97+
True
98+
True
99+
```
100+
101+
## Codebyte Example: Prioritizing Tasks
102+
103+
The following codebyte defines a class where objects are ordered by priority value:
104+
105+
```codebyte/python
106+
from functools import total_ordering
107+
108+
@total_ordering
109+
class Task:
110+
def __init__(self, name, priority):
111+
self.name = name
112+
self.priority = priority
113+
114+
def __eq__(self, other):
115+
return self.priority == other.priority
116+
117+
def __lt__(self, other):
118+
return self.priority > other.priority
119+
120+
tasks = [
121+
Task("Write report", 2),
122+
Task("Fix bugs", 5),
123+
Task("Plan sprint", 3)
124+
]
125+
126+
tasks.sort()
127+
for t in tasks:
128+
print(t.name, t.priority)
129+
```
130+
131+
Higher priority numbers are treated as greater for sorting purposes.

0 commit comments

Comments
 (0)