Jul 3, 2024
class Employee:
pass
employee_1 = Employee()
employee_2 = Employee()
employee_1.first_name = "Corey"
employee_1.last_name = "Schaefer"
employee_1.email = "corey.schaefer@company.com"
employee_1.pay = 50000
__init__
Method__init__
method:
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = f"{first}.{last}@company.com"
employee_1 = Employee("Corey", "Schaefer", 50000)
employee_2 = Employee("Test", "User", 60000)
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = f"{first}.{last}@company.com"
def full_name(self):
return f"{self.first} {self.last}"
print(employee_1.full_name()) # Output: Corey Schaefer
print(employee_2.full_name()) # Output: Test User
``
print(Employee.full_name(employee_1)) # Output: Corey Schaefer
self
self
in method definitions can lead to errors:
class Employee:
def full_name(): # Missing `self`
return f"{self.first} {self.last}"
self
as the first argument in method definitions.__init__
.