- Reduce repeated code and enhance objects using inheritance.
- Define classes that inherit from a shared parent, or superclass.
- Define methods unique to those classes.
- Define methods in the child class that use the
super
keyword to inherit from and augment methods in the parent class.
- Accomplish complex programming tasks using knowledge from previous modules.
- Inheritance: a tool that allows us to recycle code by creating a class that "inherits" the attributes and methods of a parent class.
- Composition: a tool that enables you to recycle code by adding objects to other objects. Rather than building on a base class as in inheritance, composition leverages the attributes and methods of an instance of another class.
- Subclass: a class that inherits from another class. Colloquially called a "child" class.
- Superclass: a class that is inherited by another class. Colloquially called a "parent" class.
- Child: another name for a subclass.
- Parent: another name for a superclass.
super()
: a built-in Python function that allows us to manipulate the attributes and methods of a superclass from the body of its subclass.- Decorator: syntax that allows us to add functionality to an object without modifying its structure.
In this lab, we'll be working with a school domain model. Our application has
users that are either teachers or students. Teachers and students will share
certain attributes and have certain behaviors that are unique to them. You'll be
defining a User
class that both students and teachers inherit from and you'll
be writing methods within both the Teacher
and Student
class that are unique
to that class.
This is a test-driven lab. Because "Student" comes before "Teacher"
alphabetically and we're aiming to test them in the opposite order, start off
with pytest lib/testing/user_test.py
. (If you're already in lib/
, leave it
out). Continue this process for each tested class. And
submit your work when complete.
-
Define the
User
class such that a user is instantiated with afirst_name
andlast_name
. These should be saved as attributes. -
We've given you a barebones
Teacher
class inlib/teacher.py
. Change the class definition so that theTeacher
class inherits from theUser
class. Run the test suite and notice that you are passing some tests for theTeacher
class, even without writing any code inside that class. That is because it will inherit thefirst_name
andlast_name
attributes from theUser
class you told it to inherit from. We've given you a list of knowledge strings below; modify theTeacher
class so that it initializes with this list stored as an attribute,self.knowledge
.knowledge = [ "str is a data type in Python", "programming is hard, but it's worth it", "JavaScript async web request", "Python function call definition", "object-oriented teacher instance", "programming computers hacking learning terminal", "pipenv install pipenv shell", "pytest -x flag to fail fast", ]
-
Expand the
teach()
method in theTeacher
class so that it returns a random element fromself.knowledge
. We have imported Python'srandom
library to assist you. You will want to use therandom.randint()
method to choose a random index inself.knowledge
. This method takes two arguments, a minimum number and a maximum number, and returns a random element in the range. -
We've given you a barebones
Student
class. Change the class definition so that it inherits from theUser
class. Run the test suite and notice that you are passing some tests for theStudent
class, even without writing any code inside that class. That is because it will inherit thefirst_name
andlast_name
attributes from theUser
class you told it to inherit from. -
Individual students should initialize with an attribute,
self.knowledge
, that points to an empty list. -
Expand the
learn()
method in theStudent
class so that in takes in a string and adds that string to the student'sself.knowledge
list.
We've seen how to set up inheritance to share behavior from one class to another
using parent classes as arguments in our class definition
(class Child(Parent)
), which lets the subclass use attributes and methods
that are defined on the parent class. We also discussed how class
introspection works in Python, when multiple classes define the same method.