Python OOP Video 1,2,3,4 - jude-lindale/Wiki GitHub Wiki
Python OOP Video 1: Classes and Instances
class Employee:
pass
emp_1 = Employee() """Instance of employee class"""
emp_2 = Employee()
print(emp_1)
print(emp_2)
emp_1.first = 'first name' """Instance Variables"""
emp_1.last = 'last name'
emp_1.email = 'email address'
emp_1.pay = 50000
emp_2.first = 'first name' """Instance Variables"""
emp_2.last = 'last name'
emp_2.email = 'email address'
emp_2.pay = 60000
print(emp_1.email)
print(emp_2.email)
Do not get much benefit from using classes in the way it is being used above. To make the above more usable we can make methods for the Employee class.
class Employee:
def __init__(self, fist, last, pay): """Self is the instance"""
self.fist = first
self.last = last
self.pay = pay
self.email = first + ',' + last ',' + '@company.com'
def fullname(self):
return '{} {}'.format(self.first, self.last) """{} are used as place holders, when used it will fill in for the barckets"""
emp_1 = Employee('J', 'L', 50000) """Instance of employee class"""
emp_2 = Employee('Jude', 'Lindale', 60000)
print(emp_1)
print(emp_2)
print(emp_1.email)
print(emp_2.email)
print(emp_1.fullname())
print(emp_2.fullname())