Skip to content

Commit 8906b98

Browse files
Pulkit-100OmkarPathak
authored andcommitted
Added Magic Methods (OmkarPathak#7)
1 parent a118f4a commit 8906b98

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

OOP/P11_MagicMethods.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ def __init__(self, firstname, lastname, salary = 0):
1313
def __str__(self):
1414
return 'Full Name: ' + self.firstname + ' ' + self.lastname
1515

16+
# Implements behaviour for built in type comparison to int
17+
def __int__(self):
18+
return self.salary
19+
20+
# For overloading the (==)
21+
def __eq__(self,other):
22+
return self.salary==other.salary
23+
1624
# For overloading the (+)
1725
def __add__(self, other):
1826
return self.salary + other.salary
@@ -28,3 +36,6 @@ def __mul__(self, other):
2836
print(Jagdish) # Full Name: Jagdish Pathak
2937
print(Omkar + Jagdish) # 3000 (This output because of __add__ method overloading)
3038
print(Omkar * Jagdish) # 2000000 (__mul__)
39+
print(int(Omkar)) # 1000 (__int__)
40+
print(int(Jagdish)) # 2000 (__int__)
41+
print(Omkar==Jagdish)

0 commit comments

Comments
 (0)