Friday, June 23, 2023

Python Polymorphism

 

Polymorphism

Polymorphism With A Function:

# example for polymorphism

class branch1:

          def display(self):

                    print("I am belong to T.nagar ")

class branch2:

           def display(self):

                    print("I am belong to madurai")

def myfun(ok):

                    ok.display()

B1 = branch1()

B2 = branch2()

myfun(B1)

myfun(B2)

output

I am belong to T.nagar 

I am belong to madurai

 

Python Operator Overloading

overloading the + operator using special function in python

class point:

          def __init__(self,x=0,y=0):

                    self.x = x

                    self.y = y

          def __str__(self):

                    return ("x={0},y={1})".format(self.x,self.y))

          def __add__(self,other):

                    x = self.x + other.x

                    y = self.y + other.y

                    return point(x,y)

p1 = point(2,3)

p2 = point(-1,2)

p3 = p1 + p2

print("\n addition of two object is")

print("p1({}) + p2({}) ".format (p1,p2,p3))

output

addition of two object is

p1(x=2,y=3)) + p2(x=-1,y=2))

Operator Overloading Special Function In Python

operator

Expression

Internally

Addition

P1 + p2

P1.__add__(p2)

Subtraction

P1 - p2

P1.__sub__(p2)

Multiplication

P1 * p2

P1.__mul__(p2)

Power

P1 ** p2

P1.__pow__(p2)

Division

P1 / p2

P1.__truediv__(p2)

Floor division

P1 // p2

P1.__floordiv__(p2)

Remainder(Modulo)

P1 % p2

P1.__mod__(p2)

Bitwise left shift

P1 << p2

P1.__lshift__(p2)

Bitwise right shift

P1 >> p2

P1.__rshift__(p2)

Bitwise AND

P1 & p2

P1.__and__(p2)

Bitwise OR

P1 | p2

P1.__or__(p2)

Bitwise XOR

P1 ^ p2

P1.__xor__(p2)

Bitwise NOT

  ~ P1

P1.__invert__()

 

Overloading Comparison Operator In Python

operator

expression

Internally

Less than

P1 < p2

P1.__lt__(p2)

Less than or equal to

P1 <= p2

P1.__le__(p2)

Equal to

P1 == p2

P1.__eq__(p2)

Not equal to

P1 != p2

P1.__ne__(p2)

Greater than

P1 > p2

P1.__gt__(p2)

Greater than or equal to

P1 >= p2

P1.__ge__(p2)

 

Program

class arithmetic:

   def __init__(self):

        self.a=int(input("enter first number.."))

   def __add__(self,other):

       return(self.a+other.a)

   def __sub__(self,other):

       return(self.a-other.a)

   def __mul__(self,other):

       return(self.a*other.a)

   def __truediv__(self,other):

       return(self.a/other.a)

   def __floordiv__(self,other):

       return(self.a//other.a)

   def __mod__(self,other):

       return(self.a%other.a)

 

x=arithmetic()

y=arithmetic()

while True:

          print("\n 1.addition")

          print("\n 2.subtraction")

          print("\n 3.multiplication")

          print("\n 4.division")

          print("\n 5.integer division")

          print("\n 6.remainder")

          print("\n 7.exit")

          ch=int(input("\choice ….[1..7]"))

          if  ch>=1 and ch <= 7:

                    if ch == 1: print("\n addition value=",x+y)

                    if ch == 2: print("\n subtraction value=",x-y)

                    if ch == 3: print("\n multiplication value=",x*y)

                    if ch == 4: print("\n true division value=",x/y)

                    if ch == 5: print("\n integer value=",x//y)

                    if ch == 6: print("\n remainder value=",x%y)

                    if ch == 7: break

          else:

                continue

Output

enter first number..5

enter first number..7

 

 1.addition

 

 2.subtraction

 

 3.multiplication

 

 4.division

 

 5.integer division

 

 6.remainder

 

 7.exit

choice ….[1..7]1

 

 addition value= 12

 

0 comments:

Post a Comment