-
-
Save IndhumathyChelliah/f2e1f50e18ecbe916be4b4f98f7a4a2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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