To customize the behavior of the equality operator Syntax
To use the equal to operator on custom objects, define the Let’s have a look at an example next. ExampleIn the following code, you check if a Person is equal to another Person by using the class Person: def __init__(self, age): self.age = age def __eq__(self, other): return self.age == other.age alice = Person(18) bob = Person(19) carl = Person(18) print(alice == bob) # False print(alice == carl) # True Because Alice is 18 years
old and Bob is 19 years old, the result of Background VideoPython Equal To Operator Deep Dive Default Implementation of __eq__Per default, the Because the fallback identity operator is defined for each object, you can also check equality for any two object. The following example shows that you can compare custom persons using the equality operator class Person: def __init__(self, age): self.age = age alice = Person(18) bob = Person(19) carl = Person(18) print(alice == bob) # False print(alice == carl) # False Background Video Identity OperatorTo understand the identity operator, feel free to watch the following background video: Python "is" and "is not" Operators Commutativity of Equality ==The output of You can see this in the following example: class Person: def __eq__(self, other): return False class Human: def __eq__(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. Join the free webinar now! 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. |