hw26: Reading about classes
- Due before the beginning of class on Monday, April 10
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
How much did you read carefully?
- The entire chapter
- All of the sections specifically listed above
- Some of the sections listed
- None of it too carefully
(Answer with just the letter of your choice.)
If
x
is a variable defined somewhere in our code, what doesdir(x)
show?- The directory where
x
is located - The type (class name) of
x
- The attributes and methods of
x
- The internal functions used to represent
x
- The directory where
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()
?- Error
- It runs but doesn’t print anything
- It prints
I am a green vegetable
- It prints
I am a red vegetable
Following the same example, what happens if we call
lettuce.speak()
?- Error
- It runs but doesn’t print anything
- It prints
I am a green vegetable
- It prints
I am a red vegetable
And again in the same example, what happens if we call
lettuce.ripen()
?- Error
- It runs but doesn’t print anything
- It prints
I am a green vegetable
- It prints
I am a red vegetable
Submit command
To submit files for this homework, run one of these commands:
submit -c=sd212 -p=hw26 hw26.md
club -csd212 -phw26 hw26.md
Download the file hw26.md to fill in and submit for this homework