Employees: Building a Class from Scratch
In this activity you’ll write an Employee class from scratch in Thonny, use it from a second file, and then extend it step by step. Each step tells you what to write and shows the output you should get. Type everything yourself. Don’t copy and paste.
Include your answers to the questions as comments in your python file. You’ll submit the python files through Moodle.
Before you start: make a folder called employee_project in your Documents folder. Every file in this activity goes in that folder.
Step 1: Write the Employee Class
Open Thonny. Choose File → New, then File → Save as, open your employee_project folder, and save the empty file as employee.py (all lowercase).
Now write the class, one piece at a time:
- The class header. Start a class named
Employee. Everything else in this step goes indented inside it. - The employee count. Directly inside the class (not inside any method), create a variable named
empCountand set it to0. A variable created here belongs to the whole class: there is one copy, shared by every employee. You’ll use it to count how many employees have been created. - The
__init__method. Write an__init__method with three parameters:self,name, andsalary. Inside it:- store
nameon the object asself.name - store
salaryon the object asself.salary - add 1 to the shared count. Because the count belongs to the class, refer to it as
Employee.empCount, notself.empCount.
- store
- The
displayCountmethod. Write a methoddisplayCount(self)that printsTotal Employees:followed byEmployee.empCount. - The
displayEmployeemethod. Write a methoddisplayEmployee(self)that prints this employee’s name and salary, like this:Name: Bill Yeakus Salary: 50000
Save the file and click the green Run button. If you see no error, your class is written correctly so far. (It’s normal that nothing prints. You’ll see why in Q2.)
Q1. name and salary are stored with self., but empCount is not. If you created three employees, how many separate copies of name would exist? How many copies of empCount?
Step 2: Use the Class from Another File
Create a second file (File → New) and save it as main.py in the same employee_project folder. This file will use your class.
- The first line of
main.pymust import your class from your file:from employee import Employee - Create three employee objects named
emp1,emp2, andemp3:- Bill Yeakus, salary 50000
- Sally Smith, salary 75000
- Joan Jones, salary 66000
- Call
displayEmployee()on each of the three. - Call
displayCount()on any one of them.
Save, make sure the main.py tab is selected (Thonny runs whichever tab is selected), and click Run.
Expected output
Name: Bill Yeakus Salary: 50000 Name: Sally Smith Salary: 75000 Name: Joan Jones Salary: 66000 Total Employees: 3
Q2. Select the employee.py tab and click Run. Why does nothing print, even though displayEmployee has a print statement in it?
Q3. In from employee import Employee, which word refers to your file and which refers to your class?
Step 3: Add an ID, Position, and Department
The company wants every employee to also have a position, a department, and a unique ID number. IDs start at 1 and are assigned automatically: the first employee created gets 1, the next gets 2, and so on. Nobody types an ID in.
In employee.py:
- Next to
empCount, create a second class variable namednext_idand set it to1. It remembers which ID to hand out next. - Add two parameters to
__init__, aftersalary:positionanddepartment. Store both on the object withself., the same way you stored name and salary. - Still inside
__init__, give this employee their ID: setself.idto the current value ofEmployee.next_id. - Then add 1 to
Employee.next_idso the next employee gets the next number.
In main.py, your three Employee(...) lines now need a position and department too:
| Employee | Position | Department |
|---|---|---|
| Bill Yeakus | Engineer | Development |
| Sally Smith | Manager | Sales |
| Joan Jones | Analyst | Finance |
To check the IDs, add this line to the end of main.py and run it. It should print 1 2 3.
print(emp1.id, emp2.id, emp3.id)
Q4. Why is id not one of the parameters of __init__, like position and department are?
Q5. Why must next_id be a class variable? Try changing Employee.next_id += 1 to self.next_id += 1. What IDs print now? (Change it back afterward.)
Step 4: Display an Employee with an f-string
An f-string is a string with an f right before the opening quote. Anything inside curly braces { } is replaced with its value. For example, if city is "Gambier", then f"I live in {city}!" becomes I live in Gambier!
In employee.py, rewrite displayEmployee so it uses one f-string to print all of this employee’s information in exactly this format:
ID: 1, Name: Bill Yeakus, Position: Engineer, Department: Development, Salary: $50000
In main.py, delete the print(emp1.id, ...) line and run it.
Expected output
ID: 1, Name: Bill Yeakus, Position: Engineer, Department: Development, Salary: $50000 ID: 2, Name: Sally Smith, Position: Manager, Department: Sales, Salary: $75000 ID: 3, Name: Joan Jones, Position: Analyst, Department: Finance, Salary: $66000 Total Employees: 3
Q6. Remove the f in front of your f-string and run it. What prints instead? Why? (Put the f back afterward.)
Step 5: Managers and Their Teams
Some employees are managers, and other employees report to them. You’ll give every employee a list of the people who report to them, plus two methods for working with that list.
In employee.py:
- In
__init__, create an attributeself.employeesand set it to an empty list. Every employee starts with nobody reporting to them. - Write a method
add_employee(self, employee). It should add theemployeeit’s given to this employee’sself.employeeslist, then use an f-string to print a message like:Bill Yeakus now reports to Sally Smith - Write a method
list_employees(self). It should print a heading likeSally Smith's team:, then loop throughself.employeesand calldisplayEmployee()on each one.
In main.py, add lines at the end so that Bill and Joan both report to Sally (the manager), then list Sally’s team.
Expected output (after the lines from Step 4)
Bill Yeakus now reports to Sally Smith Joan Jones now reports to Sally Smith Sally Smith's team: ID: 1, Name: Bill Yeakus, Position: Engineer, Department: Development, Salary: $50000 ID: 3, Name: Joan Jones, Position: Analyst, Department: Finance, Salary: $66000
Q7. Add a line that lists Bill’s team. What prints? Why?
Q8. The add method is called on one employee and given another employee as its argument. In emp2.add_employee(emp1), which employee is self, and which one is employee?
Wrap-Up
Q9. Your class now stores some values on self and some on the class. List which variables went where, and explain in one sentence how you decided.
