dunder method in python

technical talkiess
0

 Dunder method and abstract method:- 


class Person:

    no_name= "Test one"


    def __init__(self,name,age,salary):

        self.name = name

        self.age = age

        self.salary = salary


    def printDetails(self):

        return f"Name is {self.name} and age is {self.age} and the salary of the user is {self.salary}"


    # jo __add __ wale jo function hote hai usko hi dunder method kahte hai


    # def __add__(self,other):

    #     return self.salary + other.salary

   

    # object ko summary kare ke liye use karte hai    


    def __repr__(self):

        return self.printDetails()


    def __str__(self):

        return self.printDetails()

   

    # equal methd in the dunder method

    def __eq__(self, b):

        if self.name == b.name:

            return "equal hai bhai"

        else:

            return "Not equal hai bhai"

       

    def __lt__(self,other):

        if self.salary > other.salary:

            return "Greater"

        else:

            return "Smaller"

       

    def __mul__(self,other):

        return self.age * other.age


    def __sub__(self,other):

        return self.age - other.age


    def is_(a,b):

        return f"true" if a==b else "false"



vishal = Person("vishal",2,66666)

lakhan = Person("Rohan",2,34)


print(vishal == lakhan)


Tags

Post a Comment

0Comments

Thanks you for commenting your questions. I will see question and respond you.

Post a Comment (0)