#!/usr/bin/python
# ================================================================
# From: www.youtube.com/watch?v=rq8cL2XMM5M
# (Python OOP Tutorial 3: classmethods andstaticmethods)
# ----------------------------------------------------------------
# 1. When working with classes, regular methods automatiacally
# pass the instance as the first argument. By conventions
# it is called 'self'.
# 2. Class methods automatically pass the class as the first
# argument. By convention it is called 'cls'.
# 3. Static methods don't pass automatically pass anything.
# The don't pass the instance or the class.
# ================================================================
class Employee:
number_of_emps = 0 # class variable
raise_amt = 1.04 # class variable
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.email = first + '.' + last + 'gmail.com'
self.pay = pay
Employee.number_of_emps += 1
def fullname(self):
return '{} {}'.format(self.first,self.last)
def apply_rase(self):
self.pay = int(self.pay * self.raise_amt)
@classmethod # decorator
def set_raise_amount(cls,amount):
cls.raise_amt = amount
# create employee objests
emp_1 = Employee('Tom', 'Jones', 50000)
emp_2 = Employee('Judy', 'Smith', 60000)
# display raise amounts
print(Employee.raise_amt)
print(emp_1.raise_amt)
print(emp_2.raise_amt)
# test setting class variable (raise amount)
# (Which class (Employee) is defined by the method call)
Employee.set_raise_amount(1.05)
print(Employee.raise_amt)
print(emp_1.raise_amt)
print(emp_2.raise_amt)