{"id":156,"date":"2024-01-25T13:55:30","date_gmt":"2024-01-25T13:55:30","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=156"},"modified":"2024-01-25T13:55:31","modified_gmt":"2024-01-25T13:55:31","slug":"python-__eq__","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-__eq__\/","title":{"rendered":"Python __eq__"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use the Python&nbsp;<code>__eq__<\/code>&nbsp;method to compare two objects by their values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python&nbsp;<code>__eq__<\/code>&nbsp;method<\/h2>\n\n\n\n<p>Suppose that you have the following\u00a0<code>Person<\/code>\u00a0class with three instance attributes:\u00a0<code>first_name<\/code>,\u00a0<code>last_name<\/code>, and\u00a0<code>age<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And you create two instances of the\u00a0<code>Person<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>john = Person('John', 'Doe', 25) jane = Person('Jane', 'Doe', 25)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the\u00a0<code>john<\/code>\u00a0and\u00a0<code>jane<\/code>\u00a0objects are not the same object. And you can check it using the\u00a0<code>is<\/code>\u00a0operator:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(john is jane) <em># False<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Also, when you compare\u00a0<code>john<\/code>\u00a0with\u00a0<code>jane<\/code>\u00a0using the equal operator (==), you\u2019ll get the result of False:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(john == jane) <em># False<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Since\u00a0<code>john<\/code>\u00a0and\u00a0<code>jane<\/code>\u00a0have the same age, you want them to be equal. In other words, you want the following expression to return\u00a0<code>True<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>john == jane<\/code><\/code><\/pre>\n\n\n\n<p>To do it, you can implement the&nbsp;<code>__eq__<\/code>&nbsp;dunder method in the&nbsp;<code>Person<\/code>&nbsp;class.<\/p>\n\n\n\n<p>Python automatically calls the&nbsp;<code>__eq__<\/code>&nbsp;method of a class when you use the == operator to compare the instances of the class. By default, Python uses the&nbsp;<code>is<\/code>&nbsp;operator if you don\u2019t provide a specific implementation for the __eq__ method.<\/p>\n\n\n\n<p>The following shows how to implement the\u00a0<code>__eq__<\/code>\u00a0method in the\u00a0<code>Person<\/code>\u00a0class that returns\u00a0<code>True<\/code>\u00a0if two person objects have the same age:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def __eq__(self, other): return self.age == other.age<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Now, if you compare two instances of the\u00a0<code>Person<\/code>\u00a0class with the same age, it returns True:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>john = Person('John', 'Doe', 25) jane = Person('Jane', 'Doe', 25) print(john == jane) <em># True<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And if two instances of the Person class don\u2019t have the same age, the == operator returns False:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>john = Person('John', 'Doe', 25) mary = Person('Mary', 'Doe', 27) print(john == mary) <em># False<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following compares a\u00a0<code>Person<\/code>\u00a0object with an integer:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>john = Person('John', 'Doe', 25) print(john == 20)<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>It returns an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>AttributeError: 'int' object has no attribute 'age'<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>To fix this, you can modify the&nbsp;<code>__eq__<\/code>&nbsp;method to check if the object is an instance of the&nbsp;<code>Person<\/code>&nbsp;class before accessing the&nbsp;<code>age<\/code>&nbsp;attribute.<\/p>\n\n\n\n<p>If the other object isn\u2019t an instance of the\u00a0<code>Person<\/code>\u00a0class, the\u00a0<code>__eq__<\/code>\u00a0method returns\u00a0<code>False<\/code>, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def __eq__(self, other): if isinstance(other, Person): return self.age == other.age return False<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And you can now compare an instance of the\u00a0<code>Person<\/code>\u00a0class with an integer or any object of a different type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>john = Person('John', 'Doe', 25) print(john == 20) <em># False<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Putting it all together.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def __eq__(self, other): if isinstance(other, Person): return self.age == other.age return False john = Person('John', 'Doe', 25) jane = Person('Jane', 'Doe', 25) mary = Person('Mary', 'Doe', 27) print(john == jane) <em># True<\/em> print(john == mary) <em># False<\/em> john = Person('John', 'Doe', 25) print(john == 20) <em># False<\/em><\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use the Python&nbsp;__eq__&nbsp;method to compare two objects by their values. Introduction to the Python&nbsp;__eq__&nbsp;method Suppose that you have the following\u00a0Person\u00a0class with three instance attributes:\u00a0first_name,\u00a0last_name, and\u00a0age: And you create two instances of the\u00a0Person\u00a0class: In this example, the\u00a0john\u00a0and\u00a0jane\u00a0objects are not the same object. And you can check it using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-156","post","type-post","status-publish","format-standard","hentry","category-2-special-methods"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=156"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/156\/revisions"}],"predecessor-version":[{"id":157,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/156\/revisions\/157"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}