Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created July 3, 2020 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/f2e1f50e18ecbe916be4b4f98f7a4a2d to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/f2e1f50e18ecbe916be4b4f98f7a4a2d to your computer and use it in GitHub Desktop.
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