1. Learn
  2. /
  3. Courses
  4. /
  5. Object-Oriented Programming in Python

Connected

Exercise

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.

Instructions 1/3

undefined XP
    1
    2
    3

Define the class Employee with a constructor __init__() that:

  • accepts two arguments, name and salary (with default value0),
  • creates two attributes, also called name and salary,
  • sets their values to the corresponding arguments.