Other comparison operators #
Aside from equality operators, we can implement how our objects behave with other comparison operators. Here's a list of operators and their corresponding methods:
| Operator | Method |
|---|---|
== equal to |
__eq__(self, other) |
< less than |
__lt__(self, other) |
<= less than or equal to |
__le__(self, other) |
> greater than |
__gt__(self, other) |
>= greater than or equal to |
__ge__(self, other) |
Exercise #
- Implement all types of comparison operators for our
MyStrclass.
Tests #
MyStr("1") == MyStr("1")isTrueMyStr("1") != MyStr("1")isFalseMyStr("1") < MyStr("1")isFalseMyStr("1") > MyStr("1")isFalseMyStr("1") <= MyStr("1")isTrueMyStr("1") >= MyStr("1")isTrue
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules