Untitled Document
OOP Part - 3 - Class Methods & Static Methods
1. Here is the simple example which will change the value of class varibale raise_amt using class method Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ ***** 
# Here is the simple example which will change the value of class varibale raise_amt using class 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)

	@classmethod
	def set_raise_amt(cls, amount):
		cls.raise_amt = amount

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

Employee.set_raise_amt(1.05)

print(Employee.raise_amt)
print(emp_1.raise_amt)
print(emp_2.raise_amt)

# You can execute class method using instance ; but that doen't make any sense and this approach is not widely used 

emp_1.set_raise_amt(1.06)

print(Employee.raise_amt)
print(emp_1.raise_amt)
print(emp_2.raise_amt)
       
2. Program - clas method as a laternative constructor Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - clas method as a laternative constructor  

# In below example we will pass employee details in string format 
# We have a class method from_String method which will split this string information into variables . 
# lets run the below code and andlyse the result 

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)

	@classmethod
	def set_raise_amt(cls, amount):
		cls.raise_amt = amount

	@classmethod
	def from_string(cls, emp_str):
		first,last,pay = emp_str.split('-')
		return cls(first, last, pay)

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

emp_str_1 = 'Mrunmayee-Pande-7500'
emp_str_2 = 'Raghav-Pande-8500'

new_emp_1 = Employee.from_string(emp_str_1)

print(new_emp_1.email)
print(new_emp_1.pay)
       
3. Program - Static Method Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Static Method   

# Static Method doesn't take any cls or self as  first argument 
# it just take argument based on your method and @staticmethod decorator 

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)

	@classmethod
	def set_raise_amt(cls, amount):
		cls.raise_amt = amount

	@classmethod
	def from_string(cls, emp_str):
		first,last,pay = emp_str.split('-')
		return cls(first, last, pay)

	@staticmethod
	def is_workday(day):
		if day.weekday() == 5 or day.weekday == 6:
			return False
		return True

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

emp_str_1 = 'Mrunmayee-Pande-7500'
emp_str_2 = 'Raghav-Pande-8500'

import datetime
my_date = datetime.datetime(2020, 12, 12)
print(Employee.is_workday(my_date))
my_date = datetime.datetime(2020, 12, 14)
print(Employee.is_workday(my_date))
       
Untitled Document