Python - OOP(Object-Oriented Programming) Property Decorators - Getters, Setters, and Deleters
** OOP(Object-Oriented Programming) Property Decorators - Getters, Setters, and Deleters
==========================================
class Employee:
def __init__(self, first, last):
self.first = first
self.last = last
self.email = first + '.' + last + '@company.com'
def fullname(self):
return '{} {}'.format(self.first, self.last)
emp_1 = Employee('HAN', 'SeokDu')
print(emp_1.first)
print(emp_1.email)
print(emp_1.fullname())
---------------------------------
HAN
HAN.SeokDu@company.com
HAN SeokDu
---------------------------------
==========================================
class Employee:
def __init__(self, first, last):
self.first = first
self.last = last
self.email = first + '.' + last + '@company.com'
def fullname(self):
return '{} {}'.format(self.first, self.last)
emp_1 = Employee('HAN', 'SeokDu')
emp_1.first = 'KIM'
print(emp_1.first)
print(emp_1.email)
print(emp_1.fullname())
---------------------------------
KIM
HAN.SeokDu@company.com
KIM SeokDu
---------------------------------
==========================================
class Employee:
def __init__(self, first, last):
self.first = first
self.last = last
def email(self):
return '{} {}@company.com'.format(self.first, self.last)
def fullname(self):
return '{} {}'.format(self.first, self.last)
emp_1 = Employee('HAN', 'SeokDu')
emp_1.first = 'KIM'
print(emp_1.first)
print(emp_1.email())
print(emp_1.fullname())
---------------------------------
KIM
KIM SeokDu@company.com
KIM SeokDu
---------------------------------
==========================================
class Employee:
def __init__(self, first, last):
self.first = first
self.last = last
@property
def email(self):
return '{} {}@company.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
@fullname.setter
def fullname(self, name):
first, last = name.split(' ')
self.first = first
self.last = last
emp_1 = Employee('HAN', 'SeokDu')
emp_1.fullname = 'KIM Young'
print(emp_1.first)
print(emp_1.email)
print(emp_1.fullname)
---------------------------------
KIM
KIM Young@company.com
KIM Young
---------------------------------
==========================================
# Python Object-Oriented Programming
class Employee:
def __init__(self, first, last):
self.first = first
self.last = last
@property
def email(self):
return '{} {}@company.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
@fullname.setter
def fullname(self, name):
first, last = name.split(' ')
self.first = first
self.last = last
@fullname.deleter
def fullname(self):
print('Delete Name!')
self.first = None
self.last = None
emp_1 = Employee('HAN', 'SeokDu')
emp_1.fullname = 'KIM Young'
print(emp_1.first)
print(emp_1.email)
print(emp_1.fullname)
del emp_1.fullname
---------------------------------
KIM
KIM Young@company.com
KIM Young
Delete Name!
---------------------------------