Python - OOP(Object-Oriented Programming) Special (Magic/Dunder) Methods
** OOP(Object-Oriented Programming) Special (Magic/Dunder) Methods
==========================================
print(1 + 2)
print('a' + 'b')
---------------------------------
3
ab
---------------------------------
==========================================
class Employee:
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Employee', 20000)
print(emp_1)
---------------------------------
__main__.Employee object at 0x00C11D18
---------------------------------
==========================================
class Employee:
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.email = first + '.' + last + '@company.com'
self.pay = pay
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
def __repr__(self):
return "Employee('{}', '{}', {})".format(self.first, self.last, self.pay)
def __str__(self):
return "{} - {}".format(self.fullname(), self.email)
emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Employee', 20000)
print(repr(emp_1))
print(str(emp_1))
---------------------------------
Employee('HAN', 'SeokDu', 50000)
HAN SeokDu - HAN.SeokDu@company.com
---------------------------------
==========================================
print(1 + 2)
print(int.__add__(1,2))
print(str.__add__('a', 'b'))
print(len('test'))
print('test'.__len__())
---------------------------------
3
3
ab
4
4
---------------------------------
==========================================
class Employee:
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.email = first + '.' + last + '@company.com'
self.pay = pay
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
def __repr__(self):
return "Employee('{}', '{}', {})".format(self.first, self.last, self.pay)
def __str__(self):
return "{} - {}".format(self.fullname(), self.email)
def __add__(self, other):
return self.pay + other.pay
emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Employee', 20000)
print(emp_1 + emp_2)
---------------------------------
70000
---------------------------------
==========================================
class Employee:
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.email = first + '.' + last + '@company.com'
self.pay = pay
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
def __repr__(self):
return "Employee('{}', '{}', {})".format(self.first, self.last, self.pay)
def __str__(self):
return "{} - {}".format(self.fullname(), self.email)
# def __add__(self, other):
# return self.pay + other.pay
emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Employee', 20000)
print(emp_1 + emp_2)
---------------------------------
print(emp_1 + emp_2)
TypeError: unsupported operand type(s) for +: 'Employee' and 'Employee'
---------------------------------
==========================================
https://docs.python.org/3.8/reference/datamodel.html?highlight=__add__#emulating-numeric-types
==========================================
class Employee:
raise_amt = 1.04
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.email = first + '.' + last + '@company.com'
self.pay = pay
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
def __repr__(self):
return "Employee('{}', '{}', {})".format(self.first, self.last, self.pay)
def __str__(self):
return "{} - {}".format(self.fullname(), self.email)
def __add__(self, other):
return self.pay + other.pay
def __len__(self):
return len(self.fullname())
emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Employee', 20000)
print(len(emp_1))
---------------------------------
10
---------------------------------