Python - OOP(Object-Oriented Programming) Class Variables
** OOP(Object-Oriented Programming) Class Variables
==========================================
class Employee:
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 * 1.04)
emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)
print(emp_1.pay)
emp_1.apply_raise()
print(emp_1.pay)
---------------------------------
50000
52000
---------------------------------
==========================================
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 * raise_amount)
emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)
print(emp_1.pay)
emp_1.apply_raise()
print(emp_1.pay)
---------------------------------
emp_1.apply_raise()
self.pay = int(self.pay * raise_amount)
NameError: name 'raise_amount' is not defined
---------------------------------
==========================================
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 * Employee.raise_amount)
emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)
print(emp_1.pay)
emp_1.apply_raise()
print(emp_1.pay)
---------------------------------
50000
52000
---------------------------------
==========================================
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('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)
print(emp_1.pay)
emp_1.apply_raise()
print(emp_1.pay)
---------------------------------
50000
52000
---------------------------------
==========================================
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('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)
print(Employee.raise_amount)
print(emp_1.raise_amount)
print(emp_2.raise_amount)
---------------------------------
1.04
1.04
1.04
---------------------------------
==========================================
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('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)
print(emp_1.__dict__)
print("-"*80)
print(Employee.__dict__)
---------------------------------
{'first': 'HAN', 'last': 'SeokDu', 'pay': 50000, 'email': 'HAN.SeokDu@company.com'}
--------------------------------------------------------------------------------
{'__module__': '__main__', 'raise_amount': 1.04, '__init__':
---------------------------------
==========================================
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('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)
emp_1.raise_amount = 1.05
print(Employee.raise_amount)
print(emp_1.raise_amount)
print(emp_2.raise_amount)
---------------------------------
1.04
1.05
1.04
---------------------------------
==========================================
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('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)
emp_1.raise_amount = 1.05
print(emp_1.__dict__)
print(Employee.raise_amount)
print(emp_1.raise_amount)
print(emp_2.raise_amount)
---------------------------------
{'first': 'HAN', 'last': 'SeokDu', 'pay': 50000, 'email': 'HAN.SeokDu@company.com', 'raise_amount': 1.05}
1.04
1.05
1.04
---------------------------------
==========================================
class Employee:
num_of_emps = 0
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'
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_amount)
print(Employee.num_of_emps)
emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)
print(Employee.num_of_emps)
---------------------------------
0
2
---------------------------------