Giter VIP home page Giter VIP logo

python-p3-inheritance-lab's Introduction

Object Inheritance Lab, Part One

Learning Goals

  • 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.

Key Vocab

  • 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.

Introduction

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.


Instructions

Run the test suite to get started. This is a test-driven lab.

  1. Define the User class such that a user is instantiated with a first_name and last_name. These should be saved as attributes.

  2. We've given you a barebones Teacher class in lib/teacher.py. Change the class definition so that the Teacher class inherits from the User class. Run the test suite and notice that you are passing some tests for the Teacher class, even without writing any code inside that class. That is because it will inherit the first_name and last_name attributes from the User class you told it to inherit from. We've given you a list of knowledge strings below; modify the Teacher 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",
    ]
  3. Expand the teach() method in the Teacher class so that it returns a random element from self.knowledge. We have imported Python's random library to assist you. You will want to use the random.randint() method to choose a random index in self.knowledge. This method takes two arguments, a minimum number and a maximum number, and returns a random element in the range.

  4. We've given you a barebones Student class. Change the class definition so that it inherits from the User class. Run the test suite and notice that you are passing some tests for the Student class, even without writing any code inside that class. That is because it will inherit the first_name and last_name attributes from the User class you told it to inherit from.

  5. Individual students should initialize with an attribute, self.knowledge, that points to an empty list.

  6. Expand the learn() method in the Student class so that in takes in a string and adds that string to the student's self.knowledge list.


Conclusion

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.


Resources

python-p3-inheritance-lab's People

Contributors

professor-ben avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.