Untitled Document
OOP Part - 6 - Property Decorators - Getters,Setters and Deleters
1. Simple Example To undertsand the Concept Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ ***** 
# Simple Example To undertsand the Concept 
       
# EX - 1 PLease run the below code and check the output 
# you will see that when we chnaged the first name of employee the email output is not
# Showing expected result 

class Employee:

	def __init__(self,first,last):
		self.first = first
		self.last = last
		self.email = first + '.' + last + '@gmail.com'

	def fullname(self):
		return '{} {}'.format(self.first, self.last)

emp_1 = Employee('John', 'Smith')

emp_1.first = 'Jim'

print(emp_1.first)
print(emp_1.email)
print(emp_1.fullname())
       
2. Program - @property decorator Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - @property decorator

#  @property decorator - in order to fix above code email issue - we will use property decorator

class Employee:

	def __init__(self,first,last):
		self.first = first
		self.last = last

	@property
	def email(self):
		return '{}.{}@gmail.com'.format(self.first, self.last)
	def fullname(self):
		return '{} {}'.format(self.first, self.last)

emp_1 = Employee('John', 'Smith')

emp_1.first = 'Jim'

print(emp_1.first)
print(emp_1.email)
print(emp_1.fullname())
       
3. Program - setter method Example Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - setter method Example

class Employee:

	def __init__(self,first,last):
		self.first = first
		self.last = last

	@property
	def email(self):
		return '{}.{}@gmail.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('John', 'Smith')

emp_1.fullname = 'Mangesh pande'

print(emp_1.first)
print(emp_1.email)
print(emp_1.fullname)
    
4. Program - Deleter Method Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Deleter Method 

class Employee:

	def __init__(self,first,last):
		self.first = first
		self.last = last

	@property
	def email(self):
		return '{}.{}@gmail.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('John', 'Smith')

emp_1.fullname = 'Mangesh pande'

print(emp_1.first)
print(emp_1.email)
print(emp_1.fullname)

del emp_1.fullname
       
Untitled Document