-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomework_4.py
More file actions
161 lines (125 loc) · 4.54 KB
/
homework_4.py
File metadata and controls
161 lines (125 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Name: Laser Nite
# Kerberos: nite
# Homework_4.py
# Exercise 8.1 - Intro to Object Oriented Programming
''' 1. Local variables are those specific to, and created inside a function,
which cannot be accessed outside that function.
Object attributes, on the other hand, are data associated with a named attribute, like
a variable, that is associated with an object that represents a class.
2. the __init__(x) method
3. obj.do_something(self, attributes)
'''
# Exercise 8.2 - Understanding Objects
# 1.
class Address:
def __init__(self, number, street_name):
self.number = number
self.street_name = street_name
# 2. (a)
''' The code should print out 5:30 '''
class Clock(object):
def __init__(self, time):
self.time = time
def print_time(self):
time = '6:30'
print self.time
clock = Clock('5:30')
clock.print_time()
''' The code does indeed print out 5:30
(b) Because __init__ initializes the class with attributes self and time. Then a
print_time function is created that references and sets the object attribute self.time,
through shortened 'time', followed by a statement that prints out the object attribute
value. Then, when the object 'clock' is created with the class and a time value of
5:30, it gains the ability to use the methods of the class, and so when the
function print_time() is called, the value of self.time which has been set to '5:30'
is printed. '''
# 3. (a) The code should print out 10:30. '''
class Clock(object):
def __init__(self, time):
self.time = time
def print_time(self, time):
print time
clock = Clock('5:30')
clock.print_time('10:30')
# (b) It can be confusing, so they should have unique names.
# 4. (a) Since paris_clock is made an alias of boston_clock, 10:30 should be printed
class Clock(object):
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()
# (b) They do not act like different objects as they are aliases of eachother
# Exercise 9.1 - Designing Your Own Inheritance
class Computer:
def __init__(self, laptop, desktop, mobile, software):
self.laptop = laptop
self.desktop = desktop
self.mobile = mobile
self.software = software
class Apple(Computer):
def __init__(self, ipod):
Computer.__init__(self, laptop, desktop, mobile, software)
self.ipod = ipod
software = "OSX and iOS"
laptop = "Macbook"
desktop = "iMac"
mobile = "iPhone"
class Windows(Computer):
def __init__(self, software, game_systems):
Computer.__init__(self, laptop, desktop, mobile, software)
software = "Windows 8"
self.game_systems = game_systems
class Linux(Computer):
def __init__(self, open_source, free):
Computer.__init__(self, laptop, desktop, mobile, software)
self.open_source = open_source
self.free = free
software = "Linux"
# Exercise 9.2 - More Inheritance
# 1. Parent classes: Spell
# Child classes: Accio and Confundo
# 2. Accio
# Summoning Charm Accio
# No description
# Confundus Charm Confundo
# Causes the victim to become confused and befuddled
class Spell(object):
def __init__(self, incantation, name):
self.name = name
self.incantation = incantation
def __str__(self):
return self.name + ' ' + self.incantation + '\n' + self.get_description()
def get_description(self):
return 'No description'
def execute(self):
print self.incantation
class Accio(Spell):
def __init__(self):
Spell.__init__(self, 'Accio', 'Summoning Charm')
def get_description(self):
return 'This charm summons an object to the caster, potentilly over a \
significant distance'
class Confundo(Spell):
def __init__(self):
Spell.__init__(self, 'Confundo', 'Confundus Charm')
def get_description(self):
return 'Causes the victim to become confused and befuddled.'
def study_spell(spell):
print spell
spell = Accio()
spell.execute()
study_spell(spell)
study_spell(Confundo())
# Yay, my guesses were all correct :)
# 3. Confundo's, because __str__(self) references self.get_description() and because
# Confudo() is the child class, it's definition of a function overides the parent class
# 4. Under the class Accio should be a method that looks like:
'''
def get_description(self):
return 'This charm summons an object to the caster, potentilly over a
significant distance'
'''