6.S189 Homework 4 Written Solutions Exercise 8.1 - Intro to Object Oriented Programming 1. What is the difference between a local variable and an object's attribute? Local variables are variables used within a method; an object's attributes are variables that can be used be used throughout the entire object, regardless of the method in which it is first defined. However we usually wish to define attributes within the __init__ method. 2. What method is called when the object is created? __init__ (self, ...) 3. If you have an object instance, obj, and you want to call its do_something() method (assuming it has one), how would you do this? (write the line of code you would use) obj.do_something() ============================================================================================ Exercise 8.2 - Understanding Objects 1. Define a class called Address that has two attributes: number and street name. Make sure you have an __init__ method that initializes the object appropriately. class Address: def __init__(self, street, num): self.street_name = street self.number = num 2. Consider the following code: class Clock: def __init__(self, time): self.time = time def print_time(self): time = '6:30' print self.time clock = Clock('5:30') clock.print_time() (a) What does the code print out? If you aren't sure, create a Python file and run it. 5:30 (b) Is that what you expected? Why? Yes, because we printed out the attribute self.time, not the local variable time. 3. Consider the following code: class Clock: def __init__(self, time): self.time = time def print_time(self, time): print time clock = Clock('5:30') clock.print_time('10:30') (a) What does the code print out? If you aren't sure, create a Python file and run it. 10:30 (b) What does this tell you about giving parameters the same name as object attributes? They are needlessly confusing. It is less confusing if you give parameters, local variables, and attributes different names. 4. Consider the following code: class Clock: def __init__(self, time): self.time = time def print_time(self): print self.time boston_clock = Clock('5:30') paris_clock = boston_clock paris_clock.time = '10:30' boston_clock.print_time() (a) What does the code print out? If you aren't sure, create a Python file and run it. 10:30 (b) Why does it print what it does? (Are boston_clock and paris_clock different objects? Why or why not?) boston_clock and paris_clock are two names for the same object. This is called "aliasing." ============================================================================================ Exercise 9.1 - Designing Your Own Inheritance We accepted a wide variety of answers for this! ============================================================================================ Exercise 9.2 - More Inheritance 1. What are the parent and child classes here? The parent class is Spell. The child classes are Accio and Confundo. 2. What does the code print out? (Try figuring it out without running it in Python) Accio Summoning Charm Accio No description Confundus Charm Confundo Causes the victim to become confused and befuddled. 3. Which get_description method is called when 'study spell(Confundo())' is executed? Why? The one inside the Confundo class because methods in the child class override those in the parent class if they exist in both classes. 4. What do we need to do so that 'print Accio()' will print the appropriate description? Accio Summoning Charm This charm summons an object to the caster, potentially over a significant distance Write down the code that we need to add and/or change. Add this method to the Accio class: def get_description(self): return 'This charm summons an object to the caster, potentially over a significant distance' And change the Spell.__str__ method to reverse the order of self.name and self.incantation. (Alternatively, you can add a __str__ method to the Accio class.)