- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
Using Inheritance in Python
Modeling problems using objects is incredibly powerful, and having more specific classifications can make modeling complex problems even easier. Inheritance allows us to do this with our classes. In this hands-on lab, we'll expand the classifications we have for employees in our codebase by adding a subclass with more functionality. To feel comfortable completing this lab, you'll want to know how to create and use Python classes (watch the "Creating and Using Python Classes" video from the Certified Associate in Python Programming Certification course) and use inheritance and `super` (watch the "Inheritance and Super" video from the Certified Associate in Python Programming Certification course).
Lab Info
Table of Contents
-
Challenge
Create the `Manager` Class in a New `manager` Module
The first thing we need to do is create our class and have it inherit from the
Employeeclass. This will move our script forward. Let's create themanager.pyfile and create theManagerclass.~/manager.py
from employee import Employee class Manager(Employee): passNow if we run the
using_inheritance.pyscript, we should see the following error:$ python3.7 using_inheritance.py Traceback (most recent call last): File "using_inheritance.py", line 23, in <module> manager_1.meetings == [] AttributeError: 'Manager' object has no attribute 'meetings' -
Challenge
Add a New `meetings` Attribute That Defaults to an Empty List
The error we're running into shows we don't have a
meetingsattribute, and since we want to default this to an empty list, we'll need to customize the__init__method for our class. The method signature needs to be exactly likeEmployee, so we'll copy that over, but then we'll need to utilizesuperbefore finally adding our new attribute.~/manager.py
from employee import Employee class Manager(Employee): def __init__(self, name, email_address, title, phone_number=None): super().__init__(name, email_address, title, phone_number) self.meetings = []This should be enough to move us to the next error:
$ python3.7 using_inheritance.py Traceback (most recent call last): File "using_inheritance.py", line 32, in <module> manager_1.schedule_meeting(invitees, meeting_time1) AttributeError: 'Manager' object has no attribute 'schedule_meeting' -
Challenge
Add a `schedule_meeting` Method to the `Manager` Class
The
schedule_meetingmethod needs to take aninviteeslist and atimethat will be adatetimeobject. We will use this information to build a simple dictionary, add it tomeetings, and then sort meetings by thetimeattribute. Let's implement this method now:~/manager.py
from employee import Employee class Manager(Employee): def __init__(self, name, email_address, title, phone_number=None): super().__init__(name, email_address, title, phone_number) self.meetings = [] def schedule_meeting(self, invitees, time): self.meetings.append({"invitees": invitees, "time": time}) self.meetings.sort(key=lambda m: m["time"])Here's the next error we will see with this method implemented:
$ python3.7 using_inheritance.py Traceback (most recent call last): File "using_inheritance.py", line 42, in <module> result = manager_1.run_next_meeting() AttributeError: 'Manager' object has no attribute 'run_next_meeting' -
Challenge
Add a `run_next_meeting` Method to the `Manager` Class
The last method we need to implement is the
run_next_meetingmethod that will remove and return the first meeting dictionary in themeetingslist. Thankfully, this can easily be done by using thelist.popmethod with the argument of0:~/manager.py
from employee import Employee class Manager(Employee): def __init__(self, name, email_address, title, phone_number=None): super().__init__(name, email_address, title, phone_number) self.meetings = [] def schedule_meeting(self, invitees, time): self.meetings.append({"invitees": invitees, "time": time}) self.meetings.sort(key=lambda m: m["time"]) def run_next_meeting(self): return self.meetings.pop(0)With this implementation, we should have fully implemented everything required for the
using_inheritance.pyscript to successfully execute.$ python3.7 using_inheritance.py
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.