5/14/2020

Python - OOP(Object-Oriented Programming) classmethods and staticmethods

** OOP(Object-Oriented Programming) classmethods and staticmethods

==========================================

# Python Object-Oriented Programming

class Employee:

num_of_emps = 0
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'

Employee.num_of_emps += 1

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

def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)

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

emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)


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

---------------------------------

1.04
1.04
1.04

---------------------------------

==========================================

# Python Object-Oriented Programming

class Employee:

num_of_emps = 0
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'

Employee.num_of_emps += 1

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

def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)

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

emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)


Employee.set_raise_amt(1.05)

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

---------------------------------

1.05
1.05
1.05

---------------------------------

==========================================

# Python Object-Oriented Programming

class Employee:

num_of_emps = 0
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'

Employee.num_of_emps += 1

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

def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)

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

emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)


emp_1.set_raise_amt(1.05)



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

---------------------------------

1.05
1.05
1.05

---------------------------------

==========================================

# Python Object-Oriented Programming

class Employee:

num_of_emps = 0
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'

Employee.num_of_emps += 1

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

def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)

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

emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)

emp_str_1 = 'John-Doe-70000'
emp_str_2 = 'Steve-Smith-30000'
emp_str_3 = 'Jane-Doe-90000'

first, last, pay = emp_str_1.split('-')

new_emp_1 = Employee(first, last, pay)

print(new_emp_1.email)
print(new_emp_1.pay)

---------------------------------

John.Doe@company.com
70000

---------------------------------

==========================================

# Python Object-Oriented Programming

class Employee:

num_of_emps = 0
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'

Employee.num_of_emps += 1

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

def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)

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

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

emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)

emp_str_1 = 'John-Doe-70000'
emp_str_2 = 'Steve-Smith-30000'
emp_str_3 = 'Jane-Doe-90000'

new_emp_1 = Employee.from_string(emp_str_1)

print(new_emp_1.email)
print(new_emp_1.pay)

---------------------------------

John.Doe@company.com
70000

---------------------------------

==========================================

# Python Object-Oriented Programming

class Employee:

num_of_emps = 0
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'

Employee.num_of_emps += 1

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

def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)

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

@classmethod
def from_string(cls, emp_str):
first, last, pay = emp_str_1.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('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)

import datetime

my_date = datetime.date(2020, 5, 14)

print(Employee.is_workday(my_date))

my_date = datetime.date(2020, 5, 16)

print(Employee.is_workday(my_date))

---------------------------------

True
False

---------------------------------