👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
Python 2.x has two ways to overload comparison operators, __cmp__
or the "rich comparison operators" such as __lt__
. The rich comparison overloads are said to be preferred, but why is this so?
Rich comparison operators are simpler to implement each, but you must implement several of them with nearly identical logic. However, if you can use the builtin cmp
and tuple ordering, then __cmp__
gets quite simple and fulfills all the comparisons:
class A(object):
def __init__(self, name, age, other):
self.name = name
self.age = age
self.other = other
def __cmp__(self, other):
assert isinstance(other, A) # assumption for this example
return cmp((self.name, self.age, self.other),
(other.name, other.age, other.other))
This simplicity seems to meet my needs much better than overloading all 6(!) of the rich comparisons. (However, you can get it down to "just" 4 if you rely on the "swapped argument"/reflected behavior, but that results in a net increase of complication, in my humble opinion.)
Are there any unforeseen pitfalls I need to be made aware of if I only overload __cmp__
?
I understand the <
, <=
, ==
, etc. operators can be overloaded for other purposes, and can return any object they like. I am not asking about the merits of that approach, but only about differences when using these operators for comparisons in the same sense that they mean for numbers.
Update: As Christopher pointed out, cmp
is disappearing in 3.x. Are there any alternatives that make implementing comparisons as easy as the above __cmp__
?
👻 Read also: what is the best laptop for engineering students?
__lt__ instead of __cmp__ __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
__lt__ instead of __cmp__ __del__: Questions
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
We hope this article has helped you to resolve the problem. Apart from __lt__ instead of __cmp__, check other __del__-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano __lt__ instead of __cmp__
- Deutsch __lt__ instead of __cmp__
- Français __lt__ instead of __cmp__
- Español __lt__ instead of __cmp__
- Türk __lt__ instead of __cmp__
- Русский __lt__ instead of __cmp__
- Português __lt__ instead of __cmp__
- Polski __lt__ instead of __cmp__
- Nederlandse __lt__ instead of __cmp__
- 中文 __lt__ instead of __cmp__
- 한국어 __lt__ instead of __cmp__
- 日本語 __lt__ instead of __cmp__
- हिन्दी __lt__ instead of __cmp__
Milan | 2023-03-25
Simply put and clear. Thank you for sharing. __lt__ instead of __cmp__ and other issues with dis Python module was always my weak point 😁. Will get back tomorrow with feedback
Vigrinia | 2023-03-25
Maybe there are another answers? What __lt__ instead of __cmp__ exactly means?. Will use it in my bachelor thesis
Shanghai | 2023-03-25
operator Python module is always a bit confusing 😭 __lt__ instead of __cmp__ is not the only problem I encountered. Checked yesterday, it works!