Add a class constructor
In this exercise, you'll continue working on the Employee
class. Instead of using the methods like set_salary()
that you wrote in the previous lesson, you will introduce a constructor that assigns name and salary to the employee at the moment when the object is created.
You'll also create a new attribute -- hire_date
-- which will not be initialized through parameters, but instead will contain the current date.
Initializing attributes in the constructor is a good idea, because this ensures that the object has all the necessary attributes the moment it is created.
This exercise is part of the course
Object-Oriented Programming in Python
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
class Employee:
# Create __init__() method
____ ____(____, name, ____):
# Create the name and salary attributes
self.___ = ____
____ = ____
# From the previous lesson
def give_raise(self, amount):
self.salary += amount
def monthly_salary(self):
return self.salary/12
emp = Employee("Korel Rossi")
print(emp.name)
print(emp.salary)