- To customize the behavior of the greather than or equal to operator
x >= y, override the
__ge__()dunder method in your class definition.
- Python internally calls
x.__ge__(y)to obtain a return value when comparing two objects using
x >= y.
- The return value can be any data type because any value can automatically converted to a Boolean by using the
- If the
__ge__()method is not defined, Python will raise a
To use the greater than or equal to operator on custom objects, define the
__ge__() “dunder” magic method that takes two arguments:
other. You can then use attributes of the custom objects to determine if one is greater than or equal to the other.
The method should return a Boolean
False — however, this is not required because every object can be automatically converted to a Boolean value using the built-in
Let’s have a look at an example next.
In the following code, you compare two persons with each other by using the
age attribute as a decision criterion:
class Person: def __init__(self, age): self.age = age def __ge__(self, other): return self.age >= other.age alice = Person(18) bob = Person(17) carl = Person(18) print(alice >= bob) # True print(alice >= carl) # True print(bob >= alice) # False
For example, because Alice’s age is 18 years and Bob’s 17 years, the expression
alice >= bob evaluates to
Default Implementation of __ge__
__ge__() dunder method doesn’t have a default implementation. If you try to compare objects using the greater than or equal to operator
>=, Python will simply raise a
class Person: def __init__(self, age): self.age = age alice = Person(18) bob = Person(17) carl = Person(18) print(alice >= bob)
Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 12, in <module> print(alice >= bob) TypeError: '>=' not supported between instances of 'Person' and 'Person'
TypeError: ‘>=’ not supported between instances of ‘…’ and ‘…’
If you get the
TypeError: '>=' not supported between instances of '...' and '...', you try to compare two objects using the greater than or equal to operator
x >= y for which the
__ge__() magic method is not defined.
class Finxter: pass x = Finxter() y = Finxter() x >= y # Python will raise an error!
Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 8, in <module> x >= y # Python will raise an error! TypeError: '>=' not supported between instances of 'Finxter' and 'Finxter'
To fix the error, simply define the
__ge__(self, other) method in your class definition and return any object that will then be converted to a Boolean
class Finxter: def __ge__(self, other): return 42 x = Finxter() y = Finxter() x >= y # Now it works! # 42
Commutativity of Greater Than or Equal To >=
The output of
x >= y and
y >= x may be different because the former calls
x.__ge__(y) and the latter calls
y have different definitions of the dunder method
__ge__(), the operation becomes non-commutative.
You can see this in the following example:
class Person: def __ge__(self, other): return False class Human: def __ge__(self, other): return True alice = Person() bob = Human() print(alice >= bob) # False print(bob >= alice) # True
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.