from pprint import pprint
class HtmlDocument:
extension = 'html'
version = '5'
HtmlDocument.media_type = 'text/html'
pprint(HtmlDocument.__dict__)
__str__
__eq__
Scenario:
Suppose you want to retrieve person data with respect to their age or add new user Or simply want to check if an object is same based on certain criteria.
Handling User Input: In user interfaces or user-driven applications, you may need to compare user-provided data with existing data.
Validating login credentials by comparing a user-provided username and password with stored values.
Checking if an email address or username is already in use during user registration.
Caching and Memorization: In performance optimization techniques like caching and memorization, you can compare input parameters to previously computed results to avoid redundant calculations.
We can method override equality method from base class.
# Code without __eq__
class Person:
def __init__(self, first_name, last_name, age):
self.first_name = name
self.last_name = last_name
self.age = age
Shariq = Person("Shariq", "Khan", 28)
Akshay = Person("Akshay", "Kumar", 28)
Shariq == Akshay #False