-
Notifications
You must be signed in to change notification settings - Fork 15
/
Unary Operators.py
41 lines (32 loc) · 967 Bytes
/
Unary Operators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Unary operator overloading using magic methods.
def doc():
"""
Types of Unary operators :
opr Magic Job
- __neg__(self,others) Neg
+ __pos__(self,others) Pos
abs __abs__(self,others) Abs
~ __invert__(self,others) Invert
cmp __complex__(self,others) Complex
int __int__(self,others) Integer
long __long__(self,others) Long
float __float__(self,others) Float
oct __oct__(self,others) Octal
hex __hex__(self,others) Hexa
"""
print(doc.__doc__)
print("EXAMPLE : ")
class x:
def __init__(self, y):
self.y = y
def __neg__(self):
return self.y
def __pos__(self):
return self.y
def __invert__(self):
return self.y
if __name__ == "__main__":
obj = x(-2 * 2)
print(-obj)
print(+obj)
print(~obj)