SD 212 Spring 2024 / Homeworks


hw28: Reading about classes

  • Due before the beginning of class on Monday, April 8

Let’s read about how classes work!

This required reading is important to lay the groundwork for what we will cover next week. If you don’t read carefully, you will probably be lost during class time. If you have questions, that’s great! Bring them to class with you.

After reading, fill in the short markdown file to confirm and check your understanding.

Reading

Python for Everybody, Chapter 14: Object-Oriented Programming

You should be able to read the entire chapter. Parts of it will be review, but good to reinforce and be solid before we move forward.

If you don’t have time, here are the sections you really need to read carefully and understand:

  • Managing larger programs
  • Subdividing a problem
  • Classes as types
  • Multiple instances
  • Inheritance
  • Summary

Questions

  1. How much did you read carefully?

    1. The entire chapter
    2. All of the sections specifically listed above
    3. Some of the sections listed
    4. None of it too carefully

    (Answer with just the letter of your choice.)

  2. If x is a variable defined somewhere in our code, what does dir(x) show?

    1. The directory where x is located
    2. The type (class name) of x
    3. The attributes and methods of x
    4. The internal functions used to represent x
  3. Consider the following code:

    class Veg:
        color = 'green'
    
        def speak(self):
            print('I am a', self.color, 'vegetable')
    
    class Pepper(Veg):
        def ripen(self):
            self.color = 'red'
    
    lettuce = Veg()
    chile = Pepper()
    chile.ripen()

    What happens if we run this code and then call chile.speak()?

    1. Error
    2. It runs but doesn’t print anything
    3. It prints I am a green vegetable
    4. It prints I am a red vegetable
  4. Following the same example, what happens if we call lettuce.speak()?

    1. Error
    2. It runs but doesn’t print anything
    3. It prints I am a green vegetable
    4. It prints I am a red vegetable
  5. And again in the same example, what happens if we call lettuce.ripen()?

    1. Error
    2. It runs but doesn’t print anything
    3. It prints I am a green vegetable
    4. It prints I am a red vegetable

Submit command

To submit files for this homework, run one of these commands:

submit -c=sd212 -p=hw28 hw28.md 
club -csd212 -phw28 hw28.md
Download the file hw28.md to fill in and submit for this homework