Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created Jul 3, 2020
Embed
What would you like to do?
class Student:
def __init__(self,a,b):
self.a=a
self.b=b
def __str__(self):
return "{},{}".format(self.a,self.b)
def __neg__(self):
return -self.a,-self.b
def __pos__(self):
return +self.a,+self.b
def __abs__(self):
return abs(self.a),abs(self.b)
def __invert__(self):
return ~self.a,~self.b
# Instantiating two Student Objects
s1=Student(+15,+25)
s2=Student(-3,-5)
#Performing unary negation operator on class object.Magic method __neg__(self) is invoked
print (-s1)#Output:(-15, -25)
##Performing unary plus operator on class object.Magic method __pos__(self) is invoked
print (+s1)#Output:(15, 25)
#Performing abs() on class object.Magic methds __abs__(self) is invoked
print (abs(s2))#Output:(3, 5)
#Performing unary invert operator on class object.Magic method __invert__(self) is invoked
print (~s1)#Output:(-16, -26)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment