
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Differentiate String Operator and eq Method in Python
In Python, the comparison operator (==) and equals() methods are used in different ways when working with strings. To differentiate between the == operator and the equals method in Python we have to use them with string comparison. String comparison widely occurs when we work with strings in data analysis and machine learning. In this article, we will see how we can differentiate between the == operator and the equals() method when used with strings.
== operator in Python
The == is a comparison operator used to compare two string values. It returns True when the value of the string is equal and returns False when the value of the string is not equal. It returns true even if the strings are stored at different memory locations. It only compares the value of the string for equality.
Example
In the below example, we define two string values str1 and str2, and initialize them with the same string value. When we compare the string str1 and str2 with the == operator, it returns true as the value of both the strings are equal.
str1 = "Hello World" str2 = "Hello World" if str1 == str2: print("The strings are equal.") else: print("The strings are not equal.")
Output
The strings are equal.
__eq__() method in Python
The __eq__ method in Python is used to define how objects of a class are compared for equality. The __eq__ method takes two arguments: self(object on the left-hand side of == operator ) and other(object on the right-hand side of == operator). __eq__ method always returns a boolean value(True or False). If it returns something else other than the boolean value it will lead to TypeError.
Example
In the below example, we create a class named Person which has two attributes namely name and age. We then define the __eq__() method in the class to compare the name and age attributes of the person object. Finally, two instances of the person class are created i.e p1 and p2 and they are compared using the == operator.
class Person: def __init__(self, name, age): self.name = name self.age = age def __eq__(self, other): if isinstance(other, Person): return self.name == other.name and self.age == other.age return False p1 = Person("John", 30) p2 = Person("John", 30) if p1 == p2: print("p1 and p2 are equal")
Output
p1 and p2 are equal
Difference Between == and __eq__ method
== operator |
__eq__ method |
---|---|
== is a default behavior in python when comparing the value of two objects. |
__eq__ method needs to be explicitly defined in a class. |
== operator can be used to compare objects of different data types. |
__eq__ method can compare objects of the same type only |
It does not have much flexibility for customization. |
__eq__ method can be customized for specific types of comparison. |
It can not be inherited and should be explicitly defined for each class. |
__eq__ method can be inherited from the parent class. |
Conclusion
In this article, we discussed how the == operator and __eq__ method works in Python. == operator compares the string without looking at the memory location of the strings. __eq__ methods are defined in a class and are used to compare two objects. The article also discussed the differences between both == operator and __eq__ method.