CMU 15-112 Spring 2019: Fundamentals of Programming and Computer Science
Check 8.2


  1. Check 8.2
    Here is a very simple Student class with four properties. Write a good __eq__ method so that we can see if two objects are the same student! Note: A good __eq__ method should not crash when compared against objects of different types!

    class Student(object): def __init__(self, firstName, andrewID, year): self.firstName = firstName self.andrewID = andrewID self.year = year self.sleepy = 'probably' def __eq__(self, other): #Your code here! pass def testStudent(): s1 = Student('Mike', 'mdtaylor', 2010) s2 = Student('Mike', 'mdtaylor', 2010) s3 = Student('Jimothy', 'jbobertson', 2020) s4 = Student('Mike', 'mdtaylor', 2014) s5 = Student('Mike', 'taylorm', 2010) s6 = Student('Michael', 'mdtaylor', 2010) assert(s1 == s2) # Once you have a clone you have more time for napping s1.sleepy = 'not anymore' # It helps people tell the two of you apart assert(s1 != s2) assert(s1 != s3) assert(s2 != s4) assert(s2 != s5) assert(s2 != s6) assert(s1 != 'Kelly') testStudent() import sys def set_certificate(certificate_div_id, certificate): document[certificate_div_id].textContent = certificate def get_student_code(student_code_div_id): raw_student_code = document[student_code_div_id].textContent return window.patchCodeToCheckTimeout(raw_student_code, 'check_timeout();'); class captureIO: def __init__(self): self.captured = [] def get_output(self): out = "" for c in self.captured: out += str(c) return out def write(self, data): self.captured.append(data) def flush(self): pass def make_certificate(student_code_div_id, certificate_div_id): student_code = get_student_code(student_code_div_id) certificate = [] try: capture = captureIO() sys.stdout = capture sys.stderr = capture exec(student_code) kyra = Student("Kyra", "kbalenza", 2021) result1 = kyra == 2 result2 = kyra == "Krya" result3 = kyra == Student("Kyra", "kbalenza", 2021) a = Student("Kyra", "kbalenza", 2021) a.sleepy = "no" result4 = kyra == a output = [result1, result2, result3, result4] certificate.append((output, type(output))) except: pass set_certificate(certificate_div_id, str(certificate))

  2. Back to notes