Last active
October 25, 2021 02:54
-
-
Save msyvr/72c6b75b138cfa20a0d332be849090df to your computer and use it in GitHub Desktop.
mit 6.0001 classes - define class method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# implement and call a class method | |
class Animal(object): | |
def __init__(self, age, name = "anonymous bunny"): | |
assert type(age) == int | |
self.age = age | |
self.name = name | |
def __str__(self): | |
return "Hey! Here's an Animal of age: " + str(self.age) + " years." | |
def get_age(self): | |
return self.age | |
def set_age(self, new_age): | |
self.age = new_age | |
def get_name(self): | |
return self.name | |
def set_name(self, new_name): | |
self.name = new_name | |
class Rabbit(Animal): | |
# class variables | |
id_num = 1 # as a class variable, this is called as Rabbit.id_num | |
def __init__(self, age, parent1 = "", parent2 = ""): | |
Animal.__init__(self, age) | |
self.parent1 = parent1 | |
self.parent2 = parent2 | |
# assign a unique ID to track instances of Rabbit - use the class variable id_num | |
self.rid = Rabbit.id_num | |
Rabbit.id_num += 1 | |
def get_parent1(self): | |
return str(self.parent1) | |
def get_parent2(self): | |
return str(self.parent2) | |
def get_id(self): | |
return str(self.rid).zfill(3) | |
# define a class method: defined as "__add__", this method will be called when the "+" operator is used with instances of this class | |
def __add__(self, other): | |
return Rabbit(0, self, other) | |
def __eq__(self, other): | |
if self.parent1.rid == other.parent1.rid and self.parent2.rid == other.parent2.rid: | |
return(self.name + " and " + other.name + "have the same parents") | |
def __str__(self): | |
if self.parent1.name != "" and self.parent2.name != "": | |
return (self.parent1.name + " and " + self.parent2.name + " are now parents of bunny " + str(self.rid) + ", named " + self.name) | |
elif self.parent1.name != "": | |
return (self.parent1.name + " is now parent of bunny " + str(self.rid) + ", named " + self.name) | |
elif self.parent2.name != "": | |
return (self.parent2.name + " is now parent of bunny " + str(self.rid) + ", named " + self.name) | |
else: | |
return "There's a new bunny named " + self.name + "! Its ID is: " + str(self.rid) | |
def new_litter(parent1, parent2): | |
''' | |
create a new litter of bunnies | |
''' | |
bunny_names = list(input('What are the bunnies\' names? (single space between names): ').split(" ")) | |
for i in bunny_names: | |
new_bunny = parent1 + parent2 | |
new_bunny.set_name(i) | |
print(new_bunny) | |
print("It's bunny season!") | |
new_bunnies = [] | |
another_litter = input('Another litter? y/n: ') | |
while another_litter == 'y': | |
name_p1 = input('Expecting new bunnies!\nFirst parent\'s name: ') | |
age_p1 = int(input('Age of first parent (months): ')) | |
# here, want to check if there's an instance of Rabbit with self.name == name_p1 and self.age == age_p1, in which case that instance is used as the parent and no new (adult) Rabbit instance is created | |
p1 = Rabbit(age_p1) | |
p1.set_name(name_p1) | |
name_p2 = input('Second parent\'s name: ') | |
age_p2 = int(input('Age of second parent (months): ')) | |
p2 = Rabbit(age_p2) | |
p2.set_name(name_p2) | |
new_litter(p1, p2) | |
another_litter = input('Another litter? y/n: ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment