Tutorial Belajar Magic Method di Python
Sunday, May 14, 2017
Add Comment
Method init dan del merupakan magic method yang dimiliki oleh sistem class di Python. Dimana kamu dapat menimpa operasi suatu magic method terhadap yang kita inginkan.
Misal sekarang kita akan melakukan overriding terhadap magic method yang melakukan operasi sama dengan dan tidak sama dengan.
PRAKTEK
- Silahkan jalankan kode di sebelah kanan dan lihat hasilnya
class User:
name = ""
phone = ""
email = ""
def __init__(self, name="", phone="", email=""):
print ("User class is created...")
self.name = name
self.phone = phone
self.email = email
def __ne__(self, other):
if (self.name != other.name) or (self.phone != other.phone) or (self.email != other.email):
return True
else:
return False
def __eq__(self, other):
if (self.name == other.name) and (self.phone == other.phone) and (self.email == other.email):
return True
else:
return False
def __str__(self):
return self.name + "," + self.email +"," +self.phone
user1 = User(name="kresna", phone="7501234", email="kresnagaluh@example.com")
user2 = User(name="kresna", phone="7501234", email="kresnagaluh@example.com")
user3 = User(name="toni", phone="7504357", email="toni@example.com")
print (user1)
print (user2)
print (user3)
print (user1 == user2)
print (user1 == user3)
print (user1 != user2)
print (user1 != user3)
Hasilnya:
User class is created...
User class is created...
kresna,kresnagaluh@example.com,7501234
User class is created...
toni,toni@example.com,7504357
kresna,kresnagaluh@example.com,7501234
True
False
False
True
0 Response to "Tutorial Belajar Magic Method di Python"
Post a Comment