Jul 24, 2024
Example: Employee Class
class Employee:
pass
pass
statement to leave class empty for now.Instances of a Class
employee1 = Employee()
employee2 = Employee()
employee1.first = 'Corey'
employee1.last = 'Schaefer'
employee1.email = 'corey.schaefer@company.com'
employee1.pay = 50000
__init__
method to initialize instance variables automatically.__init__
Method (Constructor)__init__
to initialize attributes:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = f'{first}.{last}@company.com'
employee1 = Employee('Corey', 'Schaefer', 50000)
employee2 = Employee('Test', 'User', 60000)
def full_name(self):
return f'{self.first} {self.last}'
print(employee1.full_name())
self
in method definitions leads to errors:
def full_name():
return f'{self.first} {self.last}' # Error!
employee1.full_name()
Employee.full_name(employee1)
self
when using class name.__init__
method.