Untitled Document
OOP Part - 5 - Special Methods - (Magic/Dunder) Methods
1. Introduction Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ ***** 
# Introduction
       
# In Python we have special methods called as dunder methods which starts with __ . 
# In Below Example we will cover few dunder methods like __repr__ and __str__
# we will see some more practical examples on other dunder method as well 
       
2. Program - Lets run below code and print the instance ( Object created for Employee class) Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Lets run below code and print the instance ( Object created for Employee class) 

# The Below example will print the some Object reference and memory location 

class Employee:

	raise_amount = 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_amount)

emp_1 = Employee('Mangesh','Pande',50000)
emp_2 = Employee('User','Test',60000)

print(emp_1)
       
3. Program - Lets print some meaningful information for employee object Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Lets print some meaningful information for employee object 

# Here we will use __repr__ method to get the meaningful print message 

class Employee:

	raise_amount = 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_amount)

	def __repr__(self):
		return "Employee('{}', '{}', {})".format(self.first, self.last, self.pay)

emp_1 = Employee('Mangesh','Pande',50000)
emp_2 = Employee('User','Test',60000)

print(emp_1)
print(emp_2)
       
4. Program - __str__ Method Example Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - __str__  Method Example

# Here if you run - print(emp_1) it will execute __str__ method

class Employee:

	raise_amount = 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_amount)

	def __repr__(self):
		return "Employee('{}', '{}', {})".format(self.first, self.last, self.pay)

	def __str__(self):
		return '{} - {}'.format(self.fullname(), self.email)

emp_1 = Employee('Mangesh','Pande',50000)
emp_2 = Employee('User','Test',60000)

print(repr(emp_1))
print(str(emp_1))
print(emp_1)
       
5. Program - In this example we will add two eployees pay using __add__ method Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - In this example we will add two eployees pay using __add__ method

class Employee:

	raise_amount = 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_amount)

	def __repr__(self):
		return "Employee('{}', '{}', {})".format(self.first, self.last, self.pay)

	def __str__(self):
		return '{} - {}'.format(self.fullname(), self.email)

emp_1 = Employee('Mangesh','Pande',50000)
emp_2 = Employee('User','Test',60000)

#print(emp_1 + emp_2)
# Now if you run above code you will get TypeError: unsupported operand type(s) for +: 'Employee' and 'Employee'

# To Fix the above error we have added __add__ dunder method 

class Employee:

	raise_amount = 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_amount)

	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('Mangesh','Pande',50000)
emp_2 = Employee('User','Test',60000)

print(emp_1 + emp_2)

# You can get the dunder method from Puython Documentation page 
    
6. Program - Another example of __len__ method for calculating length of an aobject Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Another example of __len__ method for calculating length of an aobject

class Employee:

	raise_amount = 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_amount)

	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('Mangesh','Pande',50000)
emp_2 = Employee('User','Test',60000)

print(emp_1 + emp_2)

print(len(emp_1))
print(len(emp_2))
    
Untitled Document