-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobjects_and_inheritance.py
171 lines (127 loc) · 4.7 KB
/
objects_and_inheritance.py
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
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/env python
# -*- coding: utf-8 -*-
'''A phylogenetic hierarchy that would have made Linnaeus proud.'''
INCOMPATIBLE_SPECIES = (set(("human", "lion")),
set(("dog", "cat")),
set(("cat", "mouse")))
class Animal(object):
"""An animal from the planet Earth."""
# This just shows that an implementing class should have these attributes.
species = None
bite_strength = 0
resistance = 0
def __repr__(self):
return "{}('{}')".format(self.__class__.__name__, self.name)
def __str__(self):
description = ["Species: {}".format(self.__class__.__name__),
"Name: {}".format(self.name),
"Bite strength: {}".format(self.bite_strength),
"Resistance: {}".format(self.resistance)]
return "\n".join(description)
def __lt__(self, other):
return self.bite_strength < other.bite_strength
def __eq__(self, other):
return self.bite_strength == other.bite_strength
def __le__(self, other):
return self < other or self == other
def __gt__(self, other):
return not self <= other
def __ge__(self, other):
return self > other or self == other
def __init__(self, name):
"""An Animal is born!"""
self.name = name
def introduce(self):
"""Tell the others why you're here."""
print("My name is {} and I'm a {}.".format(self.name, self.species))
def play(self, other):
"""Play with another Animal."""
involved_species = set((self.species, other.species))
for incompatible_set in INCOMPATIBLE_SPECIES:
if involved_species == incompatible_set:
print("I hate you! I hate you so much!")
else:
print("This was fun, let's do it again soon.")
def bite(self, other):
"""You know that I could bite somebody, bite somebody like you."""
print("{}, I'm biting you this hard: {}".format(other.name,
self.bite_strength))
other.accept_bite(self.bite_strength)
def accept_bite(self, bite_strength):
"""Get bitten."""
actual_bite = bite_strength * self.resistance
print("Ouch, that hurt this much: {}".format(actual_bite))
class Human(Animal):
"""An animal that can play with others."""
species = "human"
bite_strength = 2
resistance = 2
def play(self, other):
"""Play with another being. Overrides basic play behavior in Animal."""
if other.species == "alien":
print("I don't even know how to talk to you.")
elif other.species not in ("lion", "hyena"):
print("Oh, you're such a cute {}!".format(other.species))
else:
print("Help me, it's trying to eat me!! Hellllll---")
def accept_bite(self, bite_strength):
"""React to being bitten."""
if bite_strength < 4:
print("That's it, no treat for you.")
else:
print("That was my favorite limb! :(")
class Lion(Animal):
"""A lion."""
species = "lion"
bite_strength = 9
resistance = 8
class Dog(Animal):
"""A dog."""
species = "dog"
bite_strength = 5
resistance = 3
class Cat(Animal):
"""A cat."""
species = "cat"
bite_strength = 2
resistance = 4
class Mouse(Animal):
"""A mouse."""
species = "mouse"
bite_strength = 2
resistance = 1
class Alien(object):
"""An alien."""
def __repr__(self):
return "Alien('{}')".format(self.name)
def __str__(self):
return "A mysterious - and dangerous Alien. What are his intentions?"
def __init__(self):
"""I don't even know anything about this alien."""
self.name = "SGOIWUZETÖLA"
self.species = "alien"
def introduce(self):
"""Say hi."""
print("We come in peace.")
def zap(self, other):
"""Zap somebody."""
print("Alien fry ray zap! I just fried {}!".format(other.name))
def accept_bite(self, bite_strength):
"""Get really pissed off."""
print("Take me to your leader. I'll deal with your planet afterwards.")
def main():
organism_list = [Dog("Fido"), Cat("Jerry"), Lion("Lambert"), Human("Joe"),
Alien()]
for organism in organism_list:
print(organism.__repr__())
print(organism)
dog = organism_list[0]
cat = organism_list[1]
lion = organism_list[2]
human = organism_list[3]
alien = organism_list[4]
print("Dog > Cat? {}".format(dog > cat))
print("Lion < Dog? {}".format(lion < dog))
print("Cat >= Human? {}".format(cat >= human))
if __name__ == "__main__":
main()