Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Operator Overloading in Python

The document discusses operator overloading in Python. It defines a class called 'operation' that overloads operators like +, -, *, /, // to perform arithmetic operations on objects of that class. It also defines a 'comp' class that overloads comparison operators like >, <, >= to compare objects and print results. The document then creates instances of these classes, applies the overloaded operators, and prints the results to demonstrate how operator overloading works.

Uploaded by

Jessica Dias
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Operator Overloading in Python

The document discusses operator overloading in Python. It defines a class called 'operation' that overloads operators like +, -, *, /, // to perform arithmetic operations on objects of that class. It also defines a 'comp' class that overloads comparison operators like >, <, >= to compare objects and print results. The document then creates instances of these classes, applies the overloaded operators, and prints the results to demonstrate how operator overloading works.

Uploaded by

Jessica Dias
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#operator Overloading

##+==__add__

##-==__sub__

##*==__mul__

##/==__truedivision

##//==floordivision

##>==__gt__

##<==__lt__

##<= ==__le__

##>= ==__ge__

class operation:

def __init__(self,a,b):

self.a=a

self.b=b

def __add__(self,other):

return self.a+other.a,self.b+other.b

def __sub__(self,s):

return self.a-s.a,self.b+s.b

def __mul__(self,m):

return self.a*m.a,self.b*m.b

def __truediv__(self,d):

return self.a/d.a,self.b/d.b

def __floordiv__(self,d):

return self.a//d.a,self.b//d.b

op1=operation(10,20)
op2=operation(30,40)

op3=op1+op2

print(op3)

op4=op1-op2

print(op4)

op5=op1*op2

print(op5)

op6=op1/op2

print(op6)

op7=op1//op2

print(op7)

class comp:

def __init__(self,a):

self.a=a

def __gt__(self,v):

if self.a>v.a:

print(f"Greater value {self.a}")

else:

print(f"not Greater value{self.a}")

def __lt__(self,vl):

if self.a<vl.a:

print(f"less value {self.a}")

else:

print(f"Greater value{self.a}")

def __ge__(self,ve):

if self.a>=ve.a:

print(f"greater then equal value {self.a}")


else:

print(f"less value {self.a}")

# __le__ same as __ge__

c1=comp(80)

c2=comp(70)

c1>c2

c1<c2

c1>=c2

You might also like